Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter OAuth with node-oauth for node.js+express
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));
@romartyn

This comment has been minimized.

Show comment
Hide comment
@romartyn

romartyn Oct 13, 2011

Hello everyone!
I have all required modules installed and have copypasted this code but it doesn't run..
It says " Object # has no method 'cookieDecoder' "

If I comment the line
app.use(express.cookieDecoder());
other errors appear

romartyn commented Oct 13, 2011

Hello everyone!
I have all required modules installed and have copypasted this code but it doesn't run..
It says " Object # has no method 'cookieDecoder' "

If I comment the line
app.use(express.cookieDecoder());
other errors appear

@DevelopKim

This comment has been minimized.

Show comment
Hide comment
@DevelopKim

DevelopKim Nov 17, 2011

change 'cookieParser'

DevelopKim commented Nov 17, 2011

change 'cookieParser'

@jeffrafter

This comment has been minimized.

Show comment
Hide comment
@jeffrafter
Owner

jeffrafter commented Nov 17, 2011

thanks!

@remyguercio

This comment has been minimized.

Show comment
Hide comment
@remyguercio

remyguercio Dec 18, 2011

I am new to oauth, and this is exactly what I was looking for!

remyguercio commented Dec 18, 2011

I am new to oauth, and this is exactly what I was looking for!

@glebus

This comment has been minimized.

Show comment
Hide comment
@glebus

glebus Feb 25, 2012

Object function consumer() {
return new oauth.OAuth(
"https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token",
_twitterConsumerKey, _twitterConsumerSecret, "1.0A", "http://8.8.8.8/sessions/callback", "HMAC-SHA1");
} has no method 'get'

glebus commented Feb 25, 2012

Object function consumer() {
return new oauth.OAuth(
"https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token",
_twitterConsumerKey, _twitterConsumerSecret, "1.0A", "http://8.8.8.8/sessions/callback", "HMAC-SHA1");
} has no method 'get'

@jgendr2

This comment has been minimized.

Show comment
Hide comment
@jgendr2

jgendr2 Feb 26, 2012

@glebus, having the same issue..let me know if you figure anymore out.

jgendr2 commented Feb 26, 2012

@glebus, having the same issue..let me know if you figure anymore out.

@glebus

This comment has been minimized.

Show comment
Hide comment
@glebus

glebus Feb 26, 2012

I think line 56 should look like this
consumer().get("http://twitter.com/account/verify_credentials.json", req.session.oauthAccessToken, req.session.oauthAccessTokenSecret, function (error, data, response)

glebus commented Feb 26, 2012

I think line 56 should look like this
consumer().get("http://twitter.com/account/verify_credentials.json", req.session.oauthAccessToken, req.session.oauthAccessTokenSecret, function (error, data, response)

@glebus

This comment has been minimized.

Show comment
Hide comment
@glebus

glebus Feb 26, 2012

I do not understand. As a result, displays the message:
You are signed in: undefined

Although if you look at the contents of the data object, there is a property screen_name. I have the feeling that at the time using the data object, it is still empty.

glebus commented Feb 26, 2012

I do not understand. As a result, displays the message:
You are signed in: undefined

Although if you look at the contents of the data object, there is a property screen_name. I have the feeling that at the time using the data object, it is still empty.

@jgendr2

This comment has been minimized.

Show comment
Hide comment
@jgendr2

jgendr2 Feb 26, 2012

Yea that worked for me, on line 56, consumer should be consumer() Thanks!

jgendr2 commented Feb 26, 2012

Yea that worked for me, on line 56, consumer should be consumer() Thanks!

@glebus

This comment has been minimized.

Show comment
Hide comment
@glebus

glebus Feb 28, 2012

String req.session.twitterScreenName = data["screen_name"]; (60) returns error, because data is string, not object. Need data = JSON.parse(data);

Hope this helps!

glebus commented Feb 28, 2012

String req.session.twitterScreenName = data["screen_name"]; (60) returns error, because data is string, not object. Need data = JSON.parse(data);

Hope this helps!

@santosh79

This comment has been minimized.

Show comment
Hide comment
@santosh79

santosh79 Mar 2, 2012

I'm seeing the following error while trying to require in sessions:

    throw e; // process.nextTick error, or 'error' event on first tick
          ^

Error: connect.session({ secret: "string" }) required for security

Not sure if anyone else ran into this. Would appreciate any help. Thanks.

santosh79 commented Mar 2, 2012

I'm seeing the following error while trying to require in sessions:

    throw e; // process.nextTick error, or 'error' event on first tick
          ^

Error: connect.session({ secret: "string" }) required for security

Not sure if anyone else ran into this. Would appreciate any help. Thanks.

@glebus

This comment has been minimized.

Show comment
Hide comment
@glebus

glebus Mar 2, 2012

String
app.use(express.session()); (20)
must be
app.use(express.session({secret:"secret-key"}));

glebus commented Mar 2, 2012

String
app.use(express.session()); (20)
must be
app.use(express.session({secret:"secret-key"}));

@santosh79

This comment has been minimized.

Show comment
Hide comment
@santosh79

santosh79 Mar 3, 2012

Thanks. I've created an updated gist with the suggested and have it working for anyone interested -- https://gist.github.com/1964797.

santosh79 commented Mar 3, 2012

Thanks. I've created an updated gist with the suggested and have it working for anyone interested -- https://gist.github.com/1964797.

@puppeteer701

This comment has been minimized.

Show comment
Hide comment
@puppeteer701

puppeteer701 May 4, 2013

i have a problem with this.

req.session is undefined and so I don't get accessToken and accessTokenSecret.

and dynamicHelper is no longer present in express 3, and this may be the reason why session is not defined. i am only guessing, does anyone have any ideas why req.session is undefined???

puppeteer701 commented May 4, 2013

i have a problem with this.

req.session is undefined and so I don't get accessToken and accessTokenSecret.

and dynamicHelper is no longer present in express 3, and this may be the reason why session is not defined. i am only guessing, does anyone have any ideas why req.session is undefined???

@bingeboy

This comment has been minimized.

Show comment
Hide comment
@bingeboy

bingeboy May 26, 2013

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

bingeboy commented May 26, 2013

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

@max90727

This comment has been minimized.

Show comment
Hide comment
@max90727

max90727 Jun 20, 2013

My problem is I can not get req.query.oauth_verifier

max90727 commented Jun 20, 2013

My problem is I can not get req.query.oauth_verifier

@max90727

This comment has been minimized.

Show comment
Hide comment
@max90727

max90727 Jun 20, 2013

Problem solved thanks

max90727 commented Jun 20, 2013

Problem solved thanks

@jinjojohnson

This comment has been minimized.

Show comment
Hide comment
@jinjojohnson

jinjojohnson Aug 13, 2013

super one , Thanks

jinjojohnson commented Aug 13, 2013

super one , Thanks

@mccahill

This comment has been minimized.

Show comment
Hide comment
@mccahill

mccahill Aug 29, 2013

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

mccahill commented Aug 29, 2013

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

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