twitter-application-only-auth
twitter-application-only-auth copied to clipboard
:bulb: Twitter Application Only Authentication Demo [Node.js]
Twitter Application Only Authentication Demo
Twitter supports application-only authentication giving apps the ability to issue authenticated requests on behalf of the application itself without authenticating the specific user.
Requirements
- npm (node package manager) – the default package manager for the JavaScript runtime environment Node.js
- Check they are properly installed:
node -vandnpm -v
- request – Simplified HTTP request client.
Build
- Navigate to the project directory and run the command:
npm install
- Check that
nodes_modulesfolder was created.
How To
Step 1: Create a Twitter Application
- Login into your Twitter account or create a new one.
- Go to apps.twitter.com.
- Click Create New App.
- Fill in the application details.
- Make note of the Consumer Key and Consumer Secret.
Step 2: Create a Bearer token
/*** create-twitter-bearer-token.js ***/
var request = require('request');
var consumer_key = 'CONSUMER_KEY';
var consumer_secret = 'CONSUMER_SECRET';
var encode_secret = new Buffer(consumer_key + ':' + consumer_secret).toString('base64');
var options = {
url: 'https://api.twitter.com/oauth2/token',
headers: {
'Authorization': 'Basic ' + encode_secret,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
body: 'grant_type=client_credentials'
};
request.post(options, function(error, response, body) {
console.log(body); // <<<< This is your BEARER TOKEN !!!
});
In your terminal, run the following command: node create-twitter-bearer-token.js or whatever file you named.
You now have a bearer token which allows you to make application-only API requests to many of the different REST resources that Twitter offers: https://dev.twitter.com/rest/public.
Once you got your bearer token, it will be valid for the life of your application unless you invalidate it.
Step 3: Authenticate API requests with the Bearer token
Woo hoo! Now you can make Twitter API requests by using regular 'GET' calls. Also, be sure to check the rate limits.
/*** test-nodejs.js ***/
var request = require("request");
var twitter_api = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
var bearer_token = TWITTER_BEARER_TOKEN; // the token you got in the last step
var options = {
method: 'GET',
url: twitter_api,
qs: {
"screen_name": "twitterapi"
},
json: true,
headers: {
"Authorization": "Bearer " + bearer_token
}
};
request(options, function(error, response, body) {
console.dir(body);
});