Last active
March 12, 2025 04:28
-
-
Save elmariachi111/6168585 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)
This file contains 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
var R = 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'; | |
R({ 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... | |
}); |
This file contains 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
var R = require("request"); | |
var url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; | |
var bearerToken = process.env.TWITTER_BEARER_TOKEN; //the bearer token obtained from the last script | |
R({ url: url, | |
method:'GET', | |
qs:{"screen_name":"stadolf"}, | |
json:true, | |
headers: { | |
"Authorization": "Bearer " + bearerToken | |
} | |
}, function(err, resp, body) { | |
console.dir(body); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment