Skip to content

Instantly share code, notes, and snippets.

@arshamalh
Created November 10, 2021 11:23
Show Gist options
  • Save arshamalh/c2aae7dbb1b9c1a15f90d6e6c073017c to your computer and use it in GitHub Desktop.
Save arshamalh/c2aae7dbb1b9c1a15f90d6e6c073017c to your computer and use it in GitHub Desktop.
Twitter OAuth 2.0 Get Bearer Token
// npm install got, dotenv
const got = require('got');
require('dotenv').config();
const credentials = process.env.CONSUMER_TOKEN + ":" + process.env.CONSUMER_SECRET
const credentialsBase64Encoded = new Buffer.from(credentials).toString('base64');
got({
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'
}).then((res) => {
console.log("Bearer Token: ", JSON.parse(res.body).access_token)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment