2015-10-19 230 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); 
}); 
相關問題