Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Twitter OAuth with node-oauth for node.js+express
Node.js, express, oauth example using Twitters API
Install Node.js:
curl -0
tar -zxf node-v0.6.11.tar.gz
cd node-v0.6.11
make install
Install Dependencies:
npm install express
npm install oauth
Create App File:
Save this file to app.js
Start Server:
node app.js
Navigate to the page:
Local host:
Remote host:
var express = require('express');
var util = require('util');
var oauth = require('oauth');
var app = express.createServer();
// Get your credentials here:
var _twitterConsumerKey = "twitterConsumerKey";
var _twitterConsumerSecret = "twitterConsumerSecret";
var consumer = new oauth.OAuth(
"", "",
_twitterConsumerKey, _twitterConsumerSecret, "1.0A", "", "HMAC-SHA1");
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.use(express.session({ secret: "very secret" }));
session: function(req, res){
return req.session;
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;
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 : " + util.inspect(error) + "["+oauthAccessToken+"]"+ "["+oauthAccessTokenSecret+"]"+ "["+util.inspect(results)+"]", 500);
} else {
req.session.oauthAccessToken = oauthAccessToken;
req.session.oauthAccessTokenSecret = oauthAccessTokenSecret;
app.get('/home', function(req, res){
consumer.get("", 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 parsedData = JSON.parse(data);
// req.session.twitterScreenName = response.screen_name;
res.send('You are signed in: ' + parsedData.screen_name);
app.get('*', function(req, res){
Copy link

dustinlakin commented Jul 20, 2015

For verifying credentials I ended up having to use this api point instead:

Thanks for this gist!

Copy link

JuanJo4 commented Sep 18, 2016

I've created a new gist for express 4 here

Feedback is always welcome!

Copy link

nicely commented May 23, 2018

Life saver gist! Thank you <3

Copy link

AbrahamGeorge8547 commented Aug 28, 2019

You saved my job!
How can i buy you a coffee

Copy link

joshjensen commented Aug 28, 2019

haha, happy to help @AbrahamGeorge8547! I am glad it was of use to you.

Copy link

ayzom commented May 10, 2020

Thanks again.

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