Skip to content

Instantly share code, notes, and snippets.

@dboundz
Created October 12, 2012 01:54
Show Gist options
  • Save dboundz/3876911 to your computer and use it in GitHub Desktop.
Save dboundz/3876911 to your computer and use it in GitHub Desktop.
Express Github OAuth2 flow
exports.github = function (req, res) {
res.redirect(config.Github.API_ROOT + config.Github.PATH_AUTHENTICATE + '?client_id=' + config.Github.CLIENT_ID);
};
exports.githubCallback = function (req, res) {
var code = req.query.code;
var post_data = querystring.stringify({
code: code
, client_id: config.Github.CLIENT_ID
, client_secret: config.Github.CONSUMER_SECRET
});
var post_headers = {
'Content-Type': 'application/x-www-form-urlencoded'
, 'Content-Length': post_data.length
};
var post_options = {
host: config.Github.HOSTNAME
, port: 443
, path: config.Github.PATH_ACCESS_TOKEN
, method: 'POST'
, headers: post_headers
};
var post_req = https.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.write(post_data);
post_req.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment