Skip to content

Instantly share code, notes, and snippets.

@tanepiper
Created September 11, 2010 16:14
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save tanepiper/575303 to your computer and use it in GitHub Desktop.
Save tanepiper/575303 to your computer and use it in GitHub Desktop.
A small command line nodejs script for doing Twitter OAuth.
#!/usr/bin/env node
var argv = require('optimist')
.usage('Usage: --key=[consumer key] -secret=[consumer secret]')
.demand(['key', 'secret'])
.argv
;
var OAuth = require('oauth').OAuth;
var Step = require('step');
var colors = require('colors');
var REQUEST_TOKEN_URL = 'http://api.twitter.com/oauth/request_token';
var ACCESS_TOKEN_URL = 'http://api.twitter.com/oauth/access_token';
var OAUTH_VERSION = '1.0';
var HASH_VERSION = 'HMAC-SHA1';
function getAccessToken(oa, oauth_token, oauth_token_secret, pin) {
oa.getOAuthAccessToken(oauth_token, oauth_token_secret, pin,
function(error, oauth_access_token, oauth_access_token_secret, results2) {
if (error) {
if (parseInt(error.statusCode) == 401) {
throw new Error('The pin number you have entered is incorrect'.bold.red);
}
}
console.log('Your OAuth Access Token: '.green + (oauth_access_token).bold.cyan);
console.log('Your OAuth Token Secret: '.green + (oauth_access_token_secret).bold.cyan);
console.log('Now, save these two values, along with your origional consumer secret and key and use these in your twitter app'.bold.yellow);
process.exit(1);
});
}
function getRequestToken(oa) {
oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
if(error) {
throw new Error(([error.statusCode, error.data].join(': ')).bold.red);
} else {
console.log('In your browser, log in to your twitter account. Then visit:'.bold.green)
console.log(('https://twitter.com/oauth/authorize?oauth_token=' + oauth_token).underline.green)
console.log('After logged in, you will be promoted with a pin number'.bold.green)
console.log('Enter the pin number here:'.bold.yellow);
var stdin = process.openStdin();
stdin.on('data', function(chunk) {
pin = chunk.toString().trim();
getAccessToken(oa, oauth_token, oauth_token_secret, pin);
});
}
});
}
var key = argv.key.trim();
var secret = argv.secret.trim();
var oa = new OAuth(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, key, secret, OAUTH_VERSION , null, HASH_VERSION);
getRequestToken(oa);
@liviutudor
Copy link

Note that for pin-based authentication to work, you need to pass the callback as oob not null in line 56:

 var oa = new OAuth(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, key, secret, OAUTH_VERSION , 'oob', HASH_VERSION); 

See: https://dev.twitter.com/oauth/pin-based

@soukron
Copy link

soukron commented Mar 22, 2018

This gist is still valid but it requires to use https instead of http in lines 13 and 14.

@orta
Copy link

orta commented Sep 15, 2020

Thanks! Still working well

@kirb
Copy link

kirb commented Feb 24, 2022

Realised I never posted here when I published twauth back in 2015, based on this gist. I just published v2.0.0 rewritten in TypeScript with a more friendly CLI interface. Try it with npx twauth 🙂

@tanepiper
Copy link
Author

tanepiper commented Feb 24, 2022

@kirb Oh thanks! That's great to hear that sharing this code open source led to your cli tool!

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