如何正确使用passport-oauth2来使用Twitter oauth2身份验证服务?

如何正确使用passport-oauth2来使用Twitter oauth2身份验证服务?,twitter,oauth,oauth-2.0,passport.js,Twitter,Oauth,Oauth 2.0,Passport.js,由于passport-twitter仍然适用于OAuth v1,因此我决定尝试使用passport-oauth2来完成twitter登录,而无需为我的应用程序设置会话 这就是我所尝试的: passport.use(new OAuth2Strategy({ authorizationURL: 'https://api.twitter.com/oauth/authenticate', tokenURL: 'https://api.twitter.com/oauth/access_to

由于
passport-twitter
仍然适用于OAuth v1,因此我决定尝试使用
passport-oauth2
来完成twitter登录,而无需为我的应用程序设置会话

这就是我所尝试的:

passport.use(new OAuth2Strategy({
    authorizationURL: 'https://api.twitter.com/oauth/authenticate',
    tokenURL: 'https://api.twitter.com/oauth/access_token',
    clientID: process.env.TWITTER_FINALCUT_CONSUMER_KEY,
    clientSecret: process.env.TWITTER_FINALCUT_CONSUMER_SECRET,
    callbackURL: 'http://localhost:9248/auth/login/redirect/tw'
    },
    function (access_token,refreshToken,profile,done) {
        console.log(accessToken,Object.keys(profile));
        return done(null, profile, {tokens: {accessToken: accessToken, refreshToken: refreshToken}});
}));
然而,当到达启动身份验证过程的url时,我被重定向到这个Twitter屏幕。我不知道我在做什么有问题

有什么建议吗


基本上,该策略与例如。

问题不在于Passport JS本身,而在于底层模块“node oauth”。因此,请特别注意上述策略中的“为了遵守,我们正在采取压倒一切”的评论

我更希望它能在模块中得到修复,因此我在这里发表了评论:

当这个问题解决后,我想把它作为OAuth2策略直接贡献给twitter策略

推特的首要步骤基本上是[目前,直到上述问题得到解决]:

var querystring = require('querystring');
var OAuth2 = require('oauth').OAuth2;
OAuth2.prototype.getOAuthAccessToken = function(code, params, callback) {
  var params= params || {};
  params['client_id'] = this._clientId;
  params['client_secret'] = this._clientSecret;
  var codeParam = (params.grant_type === 'refresh_token') ? 'refresh_token' : 'code';
  params[codeParam]= code;

  var post_data= querystring.stringify( params );
  var post_headers= {
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  };
  if (params.hasOwnProperty('headers') && typeof params.headers === 'object') {
    for (var key in params.headers) {
      post_headers[key] = params.headers[key];
    }
  }

  this._request("POST", this._getAccessTokenUrl() || 'https://api.twitter.com/oauth2/token' /* TODO */, post_headers, post_data, null, function(error, data, response) {
    if( error )  callback(error);
    else {
      var results;
      try {
        // As of http://tools.ietf.org/html/draft-ietf-oauth-v2-07
        // responses should be in JSON
        results= JSON.parse( data );
      }
      catch(e) {
        // .... However both Facebook + Github currently use rev05 of the spec
        // and neither seem to specify a content-type correctly in their response headers :(
        // clients of these services will suffer a *minor* performance cost of the exception
        // being thrown
        results= querystring.parse( data );
      }
      var access_token= results["access_token"];
      var refresh_token= results["refresh_token"];
      delete results["refresh_token"];
      callback(null, access_token, refresh_token, results); // callback results =-=
    }
  });
}
并且可以作为一种策略使用

var s64 = new Buffer(
  [encodeURIComponent(process.env.CONSUMER_KEY),':',
  encodeURIComponent(process.env.CONSUMER_SECRET)].join('')
).toString('base64');
OAuth2.prototype.getOAuthAccessToken('', {
  grant_type: 'client_credentials',
  headers: {
    Authorization: ['Basic', s64].join(' ')
  }
},
function(e, access_token, refresh_token, res) {
  console.log(e, access_token, refresh_token, res);
});

您是如何解决的?OAuth 2.0身份验证在使用状态时需要会话支持。如果未提供state,它将抛出错误,并显示AuthorizationError:您需要在OAuth2Strategy.authenticate处传递“state”参数。因此,passport不支持使用oauth2或其他策略(如passport-linkedin-oauth2)进行无会话身份验证。