Skip to content

Instantly share code, notes, and snippets.

@joshj
Forked from jeffrafter/server.js
Created February 28, 2012 16:58
Show Gist options
  • Save joshj/1933640 to your computer and use it in GitHub Desktop.
Save joshj/1933640 to your computer and use it in GitHub Desktop.
Twitter OAuth with node-oauth for node.js+express
/*
Node.js, express, oauth example using Twitters API
Install Node.js:
curl -0 http://nodejs.org/dist/v0.6.11/node-v0.6.11.tar.gz
tar -zxf node-v0.6.11.tar.gz
cd node-v0.6.11
./configure
make
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: http://127.0.0.1:8080
Remote host: http://yourserver.com:8080
*/
var express = require('express');
var util = require('util');
var oauth = require('oauth');
var app = express.createServer();
// Get your credentials here: https://dev.twitter.com/apps
var _twitterConsumerKey = "twitterConsumerKey";
var _twitterConsumerSecret = "twitterConsumerSecret";
var consumer = new oauth.OAuth(
"https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token",
_twitterConsumerKey, _twitterConsumerSecret, "1.0A", "http://127.0.0.1:8080/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: "very secret" }));
});
app.dynamicHelpers({
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;
res.redirect("https://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;
res.redirect('/home');
}
});
});
app.get('/home', function(req, res){
consumer.get("http://twitter.com/account/verify_credentials.json", req.session.oauthAccessToken, req.session.oauthAccessTokenSecret, function (error, data, response) {
if (error) {
res.redirect('/sessions/connect');
// 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){
res.redirect('/home');
});
app.listen(8080);
@dustinlakin
Copy link

For verifying credentials I ended up having to use this api point instead: https://api.twitter.com/1.1/account/verify_credentials.json

Thanks for this gist!

@JuanJo4
Copy link

JuanJo4 commented Sep 18, 2016

I've created a new gist for express 4 here https://gist.github.com/JuanJo4/e408d9349b403523aeb00f262900e768

Feedback is always welcome!

@nicely
Copy link

nicely commented May 23, 2018

Life saver gist! Thank you <3

@AbrahamGeorge8547
Copy link

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

@joshjensen
Copy link

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

@ayzom
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