2012-11-01 62 views
3

可能明顯和簡單,當互動。我有一個應用程序,應該訪問一個Facebook用戶的朋友信息。當用戶已經使用PassportJS驗證和我的應用程序接收到的accessToken我怎麼獲取用戶的好友信息?或者任何受保護的信息是?用戶朋友訪問的範圍參數是什麼?如何與Facebook圖形API使用PassportJS

編輯: 只是想提一下,原來的問題沒有真正回答,但答案足以讓我繼續我的調查。

我的解決辦法是使用PassportJS管理的登錄流程,當我收到的accessToken我用它爲我的Facebook圖形API調用,這是非常容易做到的。我將盡我的模塊上一些返工這個並將其發佈在GitHub上被用作是。

+0

能否請您分享一下如何正在圖形API調用? –

回答

1

爲了修改的範圍,你這樣做,當你設置你的Facebook的戰略路線。

例如,如果我想用戶的電子郵件是我的範圍的一部分,該礦將如下所示:

app.get('/auth/facebook', passport.authenticate('facebook', { scope: 'email' })); 
app.get('/auth/facebook/callback', 
    passport.authenticate('facebook', { successRedirect: '/', 
             failureRedirect: '/' })); 

我救了我的信息的MongoDB與貓鼬,但你可以很容易地將他們的朋友粘在req.user中。下面是我如何映射我的Facebook用戶的數據爲例:

passport.use(new FacebookStrategy({ 
    clientID: Common.conf.fb.appId, 
    clientSecret: Common.conf.fb.appSecret, 
    callbackURL: Common.conf.site_url + "/auth/facebook/callback" 
    }, 
    function(accessToken, refreshToken, profile, done) { 
    Model.User.findOne({uid: profile.id}, function(err, user) { 
     if (err) { return done(err); } 
     if (user) { done(null, user); } else { 
     var user_data = { 
      provider: profile.provider 
      , alias: profile.username 
      , email:  profile.emails[0].value 
      , uid:  profile.id 
      , created: new Date().getTime() 
      , name: { 
      first: profile.name.givenName 
      , last: profile.name.familyName 
      } 
      , alerts: { 
      email:  true 
      , mobile: false 
      , features: true 
      } 
     }; 
     new Model.User(user_data).save(function(err, user) { 
      if(err) { throw err; } 
      done(null, user); 
     }); 
     } 
    }); 
    } 
)); 

有時是有幫助的投入的console.log(配置文件),像這樣:

function(accessToken, refreshToken, profile, done) { 
     console.log(profile); 

,以幫助你看到原始輸出Facebook API給你的東西,並檢查你的自定義範圍變量是否存在。

+0

我看不到回答的最初問題@scott –

+0

完全同意此答案超出範圍。 – jvmvik