Skip to content

Instantly share code, notes, and snippets.

@iamdtang
Created November 29, 2017 00:44
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iamdtang/be8b4cc9558b3b6ab6cbef23150debe2 to your computer and use it in GitHub Desktop.
Save iamdtang/be8b4cc9558b3b6ab6cbef23150debe2 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 ...
});
@yOpenSource
Copy link

@skaterdav85 Nice handy snippet for OAuth 2

@HimanshuSahu31
Copy link

Upvote 👍 :-)

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