Created
July 2, 2012 03:58
-
-
Save drikin/3030992 to your computer and use it in GitHub Desktop.
Twitter OAuth Sample for node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var util = require('util'); | |
var oauth = new (require('oauth').OAuth)( | |
'https://api.twitter.com/oauth/request_token', | |
'https://api.twitter.com/oauth/access_token', | |
'xxxxxxxxxx', // consumer key | |
'xxxxxxxxxx', // consumer secret | |
'1.0', | |
null, // callback URL | |
'HMAC-SHA1' | |
); | |
oauth.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){ | |
if(error) util.puts('error :' + error) | |
else { | |
util.puts('oauth_token :' + oauth_token) | |
util.puts('oauth_token_secret :' + oauth_token_secret) | |
util.puts('requestoken results :' + util.inspect(results)) | |
util.puts("Requesting access token") | |
console.warn('https://twitter.com/oauth/authenticate?oauth_token=' + oauth_token + "\n"); | |
var stdin = process.openStdin(); | |
console.warn('PIN: '); | |
stdin.on('data', function(d) { | |
d = (d+'').trim(); | |
if(!d) { | |
console.warn('\nTry again: '); | |
} | |
console.warn('Received PIN: ' + d); | |
oauth.getOAuthAccessToken(oauth_token, oauth_token_secret, d, function(err, oauth_access_token, oauth_access_token_secret, results2) { | |
if(err) throw err; | |
console.log(results2); | |
oauth.accessToken = oauth_access_token; | |
oauth.accessTokenSecret = oauth_access_token_secret; | |
oauth.getProtectedResource("https://api.twitter.com/1/statuses/home_timeline.json", "GET", oauth_access_token, oauth_access_token_secret, function (error, data, response) { | |
util.puts(data); | |
}); | |
stdin.destroySoon(); | |
}); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank God for this. Bless your soul.