Skip to content

Instantly share code, notes, and snippets.

@rukshn
Created March 21, 2020 11:31
Show Gist options
  • Save rukshn/8b78b6410857c3cfe2f8b1563d6c784c to your computer and use it in GitHub Desktop.
Save rukshn/8b78b6410857c3cfe2f8b1563d6c784c to your computer and use it in GitHub Desktop.
A way to pin authenticate a twitter app using NodeJs and commandline
// *************
// A way to pin authenticate a twitter account using commandline and NodeJs
// Get your app consumer key and secret from dev.twitter.com
// Fill them below
// npm install to install packages
// node app.js
var oauth = require('oauth');
// Get your credentials here: https://dev.twitter.com/apps
var _twitterConsumerKey = "";
var _twitterConsumerSecret = "";
var oauthRequestToken,oauthRequestTokenSecret,oauthAccessToken,oauthAccessTokenSecret
var consumer = new oauth.OAuth(
"https://twitter.com/oauth/request_token", "https://twitter.com/oauth/access_token",
_twitterConsumerKey, _twitterConsumerSecret, "1.0A", "oob", "HMAC-SHA1");
consumer.getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){
if (error) {
res.send("Error getting OAuth request token : " + inspect(error), 500);
} else {
oauthRequestToken = oauthToken;
oauthRequestTokenSecret = oauthTokenSecret;
console.log("These are your requst tokens, no need to use them in your app");
console.log("------------------------");
console.log("<<"+oauthRequestToken);
console.log("<<"+oauthRequestTokenSecret);
console.log("https://twitter.com/oauth/authorize?oauth_token="+oauthRequestToken);
}
});
// Get process.stdin as the standard input object.
var standard_input = process.stdin;
// Set input character encoding.
standard_input.setEncoding('utf-8');
// Prompt user to input data in console.
console.log("First follow the link, open it in your browser, authorize the app. Then please input the pin you recieved in command line. Press enter");
// When user input data and click enter key.
standard_input.on('data', function (data) {
// User input exit.
if(data === 'exit\n'){
// Program exit.
console.log("User input complete, program exit.");
process.exit();
}else
{
consumer.getOAuthAccessToken(oauthRequestToken, oauthRequestTokenSecret, data, function(error, oauthAccessToken, oauthAccessTokenSecret, results) {
if (error) {
console.log('error')
// res.send("Error getting OAuth access token : " + inspect(error) + "[" + oauthAccessToken + "]" + "[" + oauthAccessTokenSecret + "]" + "[" + inspect(result) + "]", 500);
} else {
oauthAccessToken = oauthAccessToken;
oauthAccessTokenSecret = oauthAccessTokenSecret;
console.log("These are your app tokens, use this on your app")
console.log("App token <<"+oauthAccessToken)
console.log("App token secret <<"+oauthAccessTokenSecret)
console.log("Happy coding")
return
}
});
}
});
{
"name": "twitter-pin-oauth",
"version": "1.0.0",
"description": "",
"main": "py.js",
"dependencies": {
"oauth": "^0.9.15"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Rukshan <arkruka@gmail.com>",
"license": "ISC",
"private": true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment