Skip to content

Instantly share code, notes, and snippets.

@iamdtang
Forked from elmariachi111/createBearerToken.js
Last active September 30, 2018 13:28
Show Gist options
  • Save iamdtang/ff6f8c12adc0c733f521744f6e06a1b6 to your computer and use it in GitHub Desktop.
Save iamdtang/ff6f8c12adc0c733f521744f6e06a1b6 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 request = 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';
request({
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 ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment