0

I am trying to use the grant_type=password oauth flow with Twitter API. How can I implement this flow in nodejs.

var twitterConsumerKey = '';
var twitterConsumerSecret = '';

var OAuth2 = require('oauth').OAuth2;
var oauth2 = new OAuth2(
    twitterConsumerKey,
    twitterConsumerSecret,
    'https://api.twitter.com/',
    null,
    'oauth2/token',
    null
);
oauth2.getOAuthAccessToken(
    '',
    {'grant_type':'client_credentials'},
    function (e, access_token, refresh_token, results){
        console.log('bearer: ',access_token);
    }
);
user3180402
  • 579
  • 2
  • 7
  • 16

1 Answers1

1

Looks like twitter support Application Only Authentication.

I'm not familiar with the oauth module but you can use mikeal/request as follows:

var request = require('request');

request.post({
  url: 'https://api.twitter.com/',
  auth: {
    user: twitterConsumerKey,
    pass: twitterConsumerSecret
  },
  form: { grant_type:'client_credentials'},
  json: true
}, function (err, resp, body) {

  var token = body.access_token;

  //now you can use it for other APIs
  request.get({
    url: '....',
    headers: {
      Authorization: 'Bearer ' + token
    }
  }, function () {});

});
José F. Romaniello
  • 13,866
  • 3
  • 36
  • 38