Skip to content

Instantly share code, notes, and snippets.

@yssk22
Created August 14, 2010 15:30
Show Gist options
  • Save yssk22/524406 to your computer and use it in GitHub Desktop.
Save yssk22/524406 to your computer and use it in GitHub Desktop.
/**
* node.js/express twitter oauth integration sample
*/
var express = require('express'),
connect = require('connect');
var MemoryStore = require('connect/middleware/session/memory');
var OAuth= require('oauth').OAuth;
var app = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.use(connect.logger({ format: ':method :url' }));
app.use(connect.cookieDecoder());
app.use(connect.session({ store: new MemoryStore({ reapInterval: 60000 * 10 }) })),
app.use(connect.bodyDecoder());
app.use(connect.methodOverride());
app.use(connect.compiler({ src: __dirname + '/public', enable: ['less'] }));
app.use(app.router);
app.use(connect.staticProvider(__dirname + '/public'));
});
app.configure('development', function(){
app.use(connect.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(connect.errorHandler());
});
// Routes
app.get('/', function(req, res){
console.log(JSON.stringify(req.session));
res.send('dummy');
});
app.get('/login', function(req, res){
authorize(req, res);
});
app.get('/authorized', function(req, res){
authorized(req, res);
});
app.get('/twitter', function(req, res){
postToTwitter(req, res);
});
app.listen(3000);
// --- controllers
function createOAuthClient(){
return new OAuth("https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"<YOUR CONSUMER KEY>",
"<YOUR CONSUMER SECRET>",
"1.0",
'http://localhost:3000/authorized',
"HMAC-SHA1");
}
function authorize(req, res){
console.log(JSON.stringify(req.session));
var oa = createOAuthClient();
oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
console.log(error);
if(error) {
res.writeHead(500, {'Content-Type': 'text/html'});
res.send('error :' + error);
}else {
req.session.oauth = {
oauth_token: oauth_token,
oauth_token_secret: oauth_token_secret,
request_token_results: results
};
console.log(JSON.stringify(req.session));
res.redirect('http://twitter.com/oauth/authorize?oauth_token=' + oauth_token);
}
});
}
// authorized callback from twitter.com
function authorized(req, res){
var oa = createOAuthClient();
var oauth_token = req.query.oauth_token;
var oauth_verifier = req.query.oauth_verifier;
if( !req.session.oauth ){
res.redirect('/'); // invalid callback url access;
}
oa.getOauthAccessToken(
oauth_token, null, oauth_verifier,
function(error, oauth_access_token, oauth_access_token_secret, results2) {
console.log(JSON.stringify(req.session));
if(error){
console.log(error);
res.send(error);
}else{
console.log('oauth_access_token :' + oauth_access_token);
console.log('oauth_access_token_secret :' + oauth_access_token_secret);
console.log('accesstoken results :' + JSON.stringify(results2));
req.session.oauth.oauth_access_token = oauth_access_token;
req.session.oauth.oauth_access_token_secret = oauth_access_token_secret;
req.session.oauth.access_token_results = results2;
console.log(JSON.stringify(req.session));
res.redirect('/twitter');
}
});
}
function postToTwitter(req, res){
console.log(JSON.stringify(req.session));
var oa = createOAuthClient();
var text = 'foo';
console.log(req.session.oauth);
if( req.session.oauth && req.session.oauth.oauth_access_token_secret ){
var oauth_access_token = req.session.oauth.oauth_access_token;
var oauth_access_token_secret = req.session.oauth.oauth_access_token_secret;
oa.getProtectedResource(
'http://twitter.com/statuses/update.json?status=' + text,
"POST", oauth_access_token, oauth_access_token_secret,
function (error, data, response) {
console.log(JSON.stringify(req.session));
if( error ){
res.send(error);
}else{
res.send('ok');
}
});
}else{
console.log('oauth session not found');
res.redirect('/login');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment