Skip to content

Instantly share code, notes, and snippets.

Created August 28, 2010 21:37
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(
"", "",
_twitterConsumerKey, _twitterConsumerSecret, "1.0A", "", "HMAC-SHA1");
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
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;
app.get('/sessions/callback', function(req, res){
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("", 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));
Copy link

glebus commented Mar 2, 2012

app.use(express.session()); (20)
must be

Copy link

Thanks. I've created an updated gist with the suggested and have it working for anyone interested --

Copy link

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???

Copy link

use this in config to replace dynamic helper:

app.use(function(req, res){
res.locals = req.session;

See this post on stackoverflow:

Copy link

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

Copy link

Problem solved thanks

Copy link

super one , Thanks

Copy link

I have a version of this gist that works with Express 3 and shows both twitter (OAuth) and Google (OAuth2) here:

Copy link

aslamanver commented May 4, 2020

Brilliant man, Thank you ❤️

Copy link

mort3za commented Jul 3, 2021

I had to change the last url to see the result:

Copy link

ImLorio commented Dec 2, 2021

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