Created
June 25, 2011 01:34
-
-
Save takimo/1045999 to your computer and use it in GitHub Desktop.
nodejs_express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express.createServer(); | |
var Client, https, querystring; | |
https = require('https'), | |
querystring = require('querystring'); | |
app.get('/', function(req, res) { | |
//res.send('Hello World'); | |
var params = [ | |
'client_id=', | |
'response_type=code', | |
'scope=r_profile', | |
'display=pc' | |
]; | |
var url = 'https://mixi.jp/connect_authorize.pl?'; | |
res.redirect(url + params.join("&")); | |
}); | |
app.get('/callback', function(req, res){ | |
res.send(res.req.query.code); | |
var code = res.req.query.code; | |
var callback = function(status, data){ | |
}; | |
var host = 'secure.mixi-platform.com'; | |
var request = https.request({ | |
'method': 'POST', | |
'host': host, | |
'path': '/2/token', | |
'headers': { | |
'Content-Type': 'application/x-www-form-urlencoded' | |
} | |
}, function (response) { | |
var _data = ''; | |
response.setEncoding('utf8'); | |
response.on('data', function (data) { | |
_data += data; | |
}); | |
response.on('end', function () { | |
if (_data && (_data[0] === '{' || _data[0] === '[')) { | |
var data = JSON.parse(_data); | |
callback(data.error || null, data); | |
} else { | |
callback(response.statusCode !== 200, _data || null); | |
} | |
}); | |
}); | |
var request_body = [ | |
'grant_type=authorization_code', | |
'client_id=', | |
'client_secret=', | |
'code=' + code, | |
'redirect_uri=http://localhost:3000/callback', | |
]; | |
request.write(request_body.join("&")); | |
request.on('error', function (err) { | |
callback(err); | |
}); | |
request.end(); | |
}); | |
var port = process.env.PORT || 3000; | |
app.listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment