Passport-facebook仅返回'id'和'displayName'

时间:2018-05-05 07:35:35

标签: node.js passport.js passport-facebook

我使用passport-facebook来验证用户身份。在身份验证后,我只收到'id'和'displayName'参数。所有其他字段都返回为undefined。我对这封电子邮件特别感兴趣。我根本没有看到在个人资料中返回的内容。感谢任何帮助,找出我错过的东西。

配置/ 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 : '/'
    }));

Facebook战略:

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类似的代码,而且工作正常。

0 个答案:

没有答案