Created
November 10, 2021 11:23
-
-
Save arshamalh/c2aae7dbb1b9c1a15f90d6e6c073017c to your computer and use it in GitHub Desktop.
Twitter OAuth 2.0 Get Bearer Token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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