Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yOpenSource/477e0d8a227bf0e9e6a13b1f2ff091d5 to your computer and use it in GitHub Desktop.
Save yOpenSource/477e0d8a227bf0e9e6a13b1f2ff091d5 to your computer and use it in GitHub Desktop.
get bearer token from Twitter REST API for application-only authentication
require('dotenv').config();
const request = require('request');
const credentials = `${process.env.CONSUMER_KEY}:${process.env.CONSUMER_SECRET}`;
const credentialsBase64Encoded = new Buffer(credentials).toString('base64');
request({
url: 'https://api.twitter.com/oauth2/token',
method:'POST',
headers: {
'Authorization': `Basic ${credentialsBase64Encoded}`,
'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'
},
body: 'grant_type=client_credentials'
}, function(err, resp, body) {
console.log(body); // the bearer token ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment