2014-02-19 54 views
2

在護照的回購的oauth2策略使用的example,下面的函數給出:Passport.js如何使用OAuth2策略獲取配置文件數據?

passport.use(new OAuth2Strategy({ 
    authorizationURL: 'https://www.example.com/oauth2/authorize', 
    tokenURL: 'https://www.example.com/oauth2/token', 
    clientID: EXAMPLE_CLIENT_ID, 
    clientSecret: EXAMPLE_CLIENT_SECRET, 
    callbackURL: "http://localhost:3000/auth/example/callback" 
    }, 
    function(accessToken, refreshToken, profile, done) { 
    User.findOrCreate({ exampleId: profile.id }, function (err, user) { 
     return done(err, user); 
    }); 
    } 
)); 

如何護照獲得profile場?它是否由oauth端點提供了令牌?還是來自單獨的(與會話相關的)請求?

使用時,例如,Facebook的OAuth的API,用戶信息被加載自動與護照的Facebook戰略,所以我想弄清楚是如何發生的,以及如何在實施類似的行爲自定義oauth2 API。

回答

2

用戶配置文件中access_token後,通常會加載成功檢索:

https://github.com/jaredhanson/passport-oauth2/blob/master/lib/strategy.js#L175

this._oauth2.getOAuthAccessToken(code, { grant_type: 'authorization_code', redirect_uri: callbackURL }, 
     function(err, accessToken, refreshToken, params) { 
     if (err) { return self.error(self._createOAuthError('Failed to obtain access token', err)); } 

     self._loadUserProfile(accessToken, function(err, profile) { 
      if (err) { return self.error(err); } 

功能真正得到用戶信息往往是由特定的戰略提供(例如Facebook,微博,等)

在Facebook的實施:

https://github.com/jaredhanson/passport-facebook/blob/master/lib/strategy.js#L137

相關問題