Created
August 28, 2010 21:37
-
-
Save jeffrafter/555607 to your computer and use it in GitHub Desktop.
Twitter OAuth with node-oauth for node.js+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 sys = require('sys'); | |
var oauth = require('oauth'); | |
var app = express.createServer(); | |
var _twitterConsumerKey = "YOURTWITTERCONSUMERKEY"; | |
var _twitterConsumerSecret = "YOURTWITTERCONSUMERSECRET"; | |
function consumer() { | |
return new oauth.OAuth( | |
"https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token", | |
_twitterConsumerKey, _twitterConsumerSecret, "1.0A", "http://badgestar.com/sessions/callback", "HMAC-SHA1"); | |
} | |
app.configure('development', function(){ | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
app.use(express.logger()); | |
app.use(express.cookieDecoder()); | |
app.use(express.session()); | |
}); | |
app.dynamicHelpers({ | |
session: function(req, res){ | |
return req.session; | |
} | |
}); | |
app.get('/', function(req, res){ | |
res.send('Hello World'); | |
}); | |
app.get('/sessions/connect', function(req, res){ | |
consumer().getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){ | |
if (error) { | |
res.send("Error getting OAuth request token : " + sys.inspect(error), 500); | |
} else { | |
req.session.oauthRequestToken = oauthToken; | |
req.session.oauthRequestTokenSecret = oauthTokenSecret; | |
res.redirect("https://twitter.com/oauth/authorize?oauth_token="+req.session.oauthRequestToken); | |
} | |
}); | |
}); | |
app.get('/sessions/callback', function(req, res){ | |
sys.puts(">>"+req.session.oauthRequestToken); | |
sys.puts(">>"+req.session.oauthRequestTokenSecret); | |
sys.puts(">>"+req.query.oauth_verifier); | |
consumer().getOAuthAccessToken(req.session.oauthRequestToken, req.session.oauthRequestTokenSecret, req.query.oauth_verifier, function(error, oauthAccessToken, oauthAccessTokenSecret, results) { | |
if (error) { | |
res.send("Error getting OAuth access token : " + sys.inspect(error) + "["+oauthAccessToken+"]"+ "["+oauthAccessTokenSecret+"]"+ "["+sys.inspect(results)+"]", 500); | |
} else { | |
req.session.oauthAccessToken = oauthAccessToken; | |
req.session.oauthAccessTokenSecret = oauthAccessTokenSecret; | |
// Right here is where we would write out some nice user stuff | |
consumer.get("http://twitter.com/account/verify_credentials.json", req.session.oauthAccessToken, req.session.oauthAccessTokenSecret, function (error, data, response) { | |
if (error) { | |
res.send("Error getting twitter screen name : " + sys.inspect(error), 500); | |
} else { | |
req.session.twitterScreenName = data["screen_name"]; | |
res.send('You are signed in: ' + req.session.twitterScreenName) | |
} | |
}); | |
} | |
}); | |
}); | |
app.listen(parseInt(process.env.PORT || 80)); |
My problem is I can not get req.query.oauth_verifier
Problem solved thanks
super one , Thanks
I have a version of this gist that works with Express 3 and shows both twitter (OAuth) and Google (OAuth2) here: https://gist.github.com/mccahill/6378276
Brilliant man, Thank you ❤️
I had to change the last url to see the result: https://api.twitter.com/1.1/account/verify_credentials.json
TypeError: Cannot set properties of undefined (setting 'oauthRequestToken')
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use this in config to replace dynamic helper:
app.use(function(req, res){
res.locals = req.session;
});
See this post on stackoverflow:
http://stackoverflow.com/questions/13517375/nodejs-express-dynamichelpers-error