如何在自治客户端中获取令牌(用户名 - 密码流)

时间:2014-02-14 11:41:01

标签: node.js twitter-oauth

我正在尝试将grant_type=password oauth流与Twitter API一起使用。如何在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);
    }
);

1 个答案:

答案 0 :(得分:0)

看起来像twitter支持Application Only Authentication

我不熟悉oauth模块,但您可以使用mikeal/request,如下所示:

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 () {});

});