首页 > 解决方案 > Passport-facebook 仅返回 'id' 和 'displayName'

问题描述

我正在使用 passport-facebook 来验证用户身份。身份验证后,我只收到“id”和“displayName”参数。所有其他字段都以未定义的形式返回。我对电子邮件特别感兴趣。我根本看不到配置文件中返回的内容。感谢任何帮助找出我所缺少的东西。

配置/auth.js:

module.exports = {

    'facebookAuth' : {
        clientID: 'MY_CLIENT_ID', // your App ID
        clientSecret: 'MY_CLIENT_SECRET', // your App Secret
        callbackURL: '/auth/facebook/callback',
        profileFields: ['id', 'emails', 'displayName']
    },

我也尝试将“电子邮件”更改为“电子邮件”

路线:

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

脸书策略:

passport.use(new FacebookStrategy({
    clientID        : configAuth.facebookAuth.clientID,
    clientSecret    : configAuth.facebookAuth.clientSecret,
    callbackURL     : configAuth.facebookAuth.callbackURL
},

function(token, refreshToken, profile, done) {
    process.nextTick(function() {
        console.log(util.inspect(profile, {showHidden: false, depth: null}))
        User.findOne({ 'facebook.id' : profile.id }, function(err, user) {
            if (err)
                return done(err);

            if (user) {
                return done(null, user); // user found, return that user
            } else {
                // if there is no user found with that facebook id, create them
                var newUser            = new User();

                newUser.facebook.id    = profile.id; // set the users facebook id
                newUser.facebook.token = token; // we will save the token that facebook provides to the user
                newUser.facebook.name  = profile.displayName; // look at the passport user profile to see how names are returned
                // newUser.facebook.email = profile.emails[0].value; // facebook can return multiple emails so we'll take the first

                // save our user to the database
                newUser.save(function(err) {
                    if (err)
                        throw err;

                    // if successful, return the new user
                    return done(null, newUser);
                });
            }

        });
    });

}));

返回的配置文件信息:

GET /auth/facebook 302 0.744 ms - 0
{ id: '10156351538257640',
  username: undefined,
  displayName: 'Nitin Vig',
  name: 
   { familyName: undefined,
     givenName: undefined,
     middleName: undefined },
  gender: undefined,
  profileUrl: undefined,
  provider: 'facebook',
  _raw: '{"name":"Nitin Vig","id":"10156351538257640"}',
  _json: { name: 'Nitin Vig', id: '10156351538257640' } }
GET /auth/facebook/callback?code=AQAdi8Gs55Th1oXVXGLxNynm8s_rYs9XWC7IRB4hCk5xoVWCgPsRXS-DL6Q6ekI0bvb5osls77OWSfuTdCAIhkc2ntohRrG6ZDES3Nx907FdpjMzJzfCYJfXYeutBAkPwTbesjdcBsig_76VFWwV9WL-6X0jLngTtYLdkELWid9G5bTltc0HrmUEaVkf8w6LeaDHdbQ1tlxh5CP9P9W7vT_M6AVXSpGPFTaoLI8IO25jyS-Xm9bIprPQV1-eVYK_EHhcYZVaK2INnGeWhwTu-6P1MMFO4gPIFGgiKqSmREXnfS4C_JqgwpjEc0Z5PGbhLIE 302 16582.309 ms - 60
GET /profile 200 2.081 ms - 2204

我正在使用与 passport-google-oauth 类似的代码,并且效果很好。

标签: node.jspassport.jspassport-facebook

解决方案


推荐阅读