Skip to content

Instantly share code, notes, and snippets.

@elmariachi111
Last active January 13, 2020 15:44
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save elmariachi111/6168585 to your computer and use it in GitHub Desktop.
Save elmariachi111/6168585 to your computer and use it in GitHub Desktop.
A Javascript file that requests a Twitter bearer token for application only authentication (https://dev.twitter.com/docs/auth/application-only-auth). Depends on mikeals request library (https://github.com/mikeal/request) (npm install request)
var R = require("request");
var key = process.env.TWITTER_CONSUMER_KEY;
var secret = process.env.TWITTER_CONSUMER_SECRET;
var cat = key +":"+secret;
var credentials = new Buffer(cat).toString('base64');
var url = 'https://api.twitter.com/oauth2/token';
R({ url: url,
method:'POST',
headers: {
"Authorization": "Basic " + credentials,
"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8"
},
body: "grant_type=client_credentials"
}, function(err, resp, body) {
console.dir(body); //the bearer token...
});
var R = require("request");
var url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
var bearerToken = process.env.TWITTER_BEARER_TOKEN; //the bearer token obtained from the last script
R({ url: url,
method:'GET',
qs:{"screen_name":"stadolf"},
json:true,
headers: {
"Authorization": "Bearer " + bearerToken
}
}, function(err, resp, body) {
console.dir(body);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment