2017-09-01 87 views
0

我使用的护照Facebook的插件和我的测试脚本定义的策略如下:护照Facebook的策略在NextJS应用程序返回undefined值

// Configure Passport 
// --------------------------------------------------------------- 
passport.use(new LocalStrategy(User.authenticate())); 
passport.use(new FacebookStrategy({ 
    clientID: process.env.FACEBOOK_APP_ID, 
    clientSecret: process.env.FACEBOOK_APP_SECRET, 
    callbackURL: process.env.FACEBOOK_APP_CALLBACK, 
}, 
    function (accessToken, refreshToken, profile, done) { 
    console.log(profile); 
    } 
)); 
passport.serializeUser(User.serializeUser()); 
passport.deserializeUser(User.deserializeUser()); 
// --------------------------------------------------------------- 

我的认证途径是:

// Facebook 
router.get('/auth/facebook', passport.authenticate('facebook')); 
router.get('/facebook/callback', 
    passport.authenticate('facebook', { 
    successRedirect: '/success', 
    failureRedirect: '/failure', 
    }), 
); 

我现在所要做的就是显示Facebook成功验证后返回的所有值。认证工作正常但Facebook似乎在profile对象被返回undefined大多数键:

{ id: '1234567812345677', 
    username: undefined, 
    displayName: 'John Doe', 
    name: 
    { familyName: undefined, 
    givenName: undefined, 
    middleName: undefined }, 
    gender: undefined, 
    profileUrl: undefined, 
    provider: 'facebook', 
    _raw: '{"name":"John Doe","id":"1234567812345677"}', 
    _json: { name: 'John Doe', id: '1234567812345677' } } 

我得到的是displayNameid;其他一切都是undefined。出了什么问题?之前在这里提出了一个非常类似的问题,但没有一个答案(没有被接受的答案)提供了解决问题的方法。

+0

完成guess-检查您的应用范围。您可能没有完整的个人资料信息的权限。 –

+0

我没有为我的认证调用指定任何明确的'范围'。不应该像* username *那样可以作为默认范围的一部分?我在网上遇到的所有教程(包括PassportJS文档)都会指示您指定一个范围,以便您需要任何其他访问权限,例如时间线等。 – TheLearner

+0

您是否找到了解决方案?我得到了同样的问题:/ –

回答

0

我没有这方面的经验,但从看facebook-passport的readme,听起来好像你需要在你的FacebookStrategy中使用profileFields。 :

The Facebook profile contains a lot of information about a user. By default, not all the fields in a profile are returned. The fields needed by an application can be indicated by setting the profileFields option.

new FacebookStrategy({ 
    clientID: FACEBOOK_APP_ID, 
    clientSecret: FACEBOOK_APP_SECRET, 
    callbackURL: "http://localhost:3000/auth/facebook/callback", 
    profileFields: ['id', 'displayName', 'photos', 'email'] 
}), ...) 

Refer to the User section of the Graph API Reference for the complete set of available fields.

+1

不幸的是,这是行不通的。抛出错误: 错误 at /home/ubuntu/nano/node_modules/passport-facebook/lib/strategy.js:165:21 at passBackControl(/ home/ubuntu/nano/node_modules/oauth/lib/oauth2。 js:132:9) IncomingMessage。 (/home/ubuntu/nano/node_modules/oauth/lib/oauth2.js:157:7) at emitNone(events.js:110:20) at IncomingMessage.emit(events.js:207:7) process.tickCallback(internal/process/next_tick.js:161:9)在endReadableNT(_stream_readable.js:1047:12) at _combinedTickCallback(internal/process/next_tick.js:102:11) – TheLearner