2015-10-19 53 views
2

由于passport-twitter仍然适用于OAuth v1,我决定尝试与passport-oauth2完成Twitter登录与我的应用程序的会话。如何正确使用passport-oauth2使用Twitter Oauth2身份验证服务?

这是我的尝试:

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

但是到达网址用于启动身份验证过程时,我重定向到这个Twitter的屏幕。我无法弄清楚我在做什么。

有什么建议吗?

enter image description here

+0

你是如何解决的呢? – mathieug

+0

使用状态时,OAuth 2.0身份验证需要会话支持。如果未提供状态,则会抛出错误,指出AuthorizationError:您需要在OAuth2Strategy.authenticate处传递“状态”参数 。因此,护照不支持使用oauth2或其他策略(如passport-linkedin-oauth2)的会话认证。 –

回答

0

基本上策略将是相同的,作为例如 https://github.com/Slotos/passport-reddit/blob/master/lib/passport-reddit/strategy.js

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

我宁愿如果将它固定在模块中,所以我只是在这里说:https://github.com/ciaranj/node-oauth/issues/300

问题解决后,我想直接有助于它在Twitter的战略作为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); 
}); 
相关问题