Skip to content

Instantly share code, notes, and snippets.

@esperia
Forked from jeffrafter/server.js
Created December 2, 2011 11:59
Show Gist options
  • Save esperia/1422995 to your computer and use it in GitHub Desktop.
Save esperia/1422995 to your computer and use it in GitHub Desktop.
Twitter OAuth with node-oauth for node.js+express
var express = require('express');
var util = require('util');
var oauth = require('oauth');
var app = express.createServer();
var _twitterConsumerKey = "YOURTWITTERCONSUMERKEY";
var _twitterConsumerSecret = "YOURTWITTERCONSUMERSECRET";
function consumer() {
return new oauth.OAuth(
"https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/access_token",
_twitterConsumerKey, _twitterConsumerSecret, "1.0A", "http://127.0.0.1:3000/sessions/callback", "HMAC-SHA1");
}
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.session({ secret: 'foo_bar-session-secret' }));
});
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 : " + util.inspect(error), 500);
} else {
req.session.oauthRequestToken = oauthToken;
req.session.oauthRequestTokenSecret = oauthTokenSecret;
res.redirect("https://api.twitter.com/oauth/authorize?oauth_token="+req.session.oauthRequestToken);
}
});
});
app.get('/sessions/callback', function(req, res){
util.puts(">>"+req.session.oauthRequestToken);
util.puts(">>"+req.session.oauthRequestTokenSecret);
util.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 : " + util.inspect(error) + "["+oauthAccessToken+"]"+ "["+oauthAccessTokenSecret+"]"+ "["+util.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 : " + util.inspect(error), 500);
} else {
var verify_credentials = JSON.parse(data);
req.session.twitterScreenName = verify_credentials["name"];
res.send('You are signed in: ' + req.session.twitterScreenName)
}
});
}
});
});
app.listen(parseInt(process.env.PORT || 3000));
@sedley
Copy link

sedley commented Jul 26, 2013

You need to change line 56 to read

consumer().get("http://api.twitter.com/1.1/account/verify_credentials.json", req.session.oauthAccessToken,        req.session.oauthAccessTokenSecret, function (error, data, response) {

to adjust for the change to REST V1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment