且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Passport-Facebook身份验证不会为所有Facebook帐户提供电子邮件

更新时间:2023-09-08 23:49:34

确保代码中包含以下两项内容:

Make sure these two things are in your code:

  passport.use(new FacebookStrategy({
            clientID: 'CLIENT_ID',
            clientSecret: 'CLIENT_SECRET',
            callbackURL: "http://www.example.com/auth/facebook/callback"
            passReqToCallback : true,
            profileFields: ['id', 'emails', 'name'] //This
        },

这个:

app.get('/connect/facebook', passport.authorize('facebook', { scope : ['email'] }));

这使您可以访问以下内容:

This gives you access to the following:


  • profile.id

  • profile.name.givenName

  • profile.name.familyName

  • pro file.emails

  • profile.id
  • profile.name.givenName
  • profile.name.familyName
  • profile.emails

最后一个是数组,所以使用 profile.emails [0] .value 获取用户的第一个电子邮件地址。

The last one being an array, so use profile.emails[0].value to get the first email address of the user.

As shamim reza 指出,您可能想检查 profile.emails!== undefined ,因为该属性仅在用户拥有至少一个经过验证的电子邮件地址时才存在。

As shamim reza pointed out, you might want to check if profile.emails !== undefined because the property only exists if the user has at least one verified email address.