Facebook API不再为使用护照FB策略的用户登录提供电子邮件

时间:2016-01-22 03:04:23

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

我正在使用护照的FB策略以及本地登录来构建express4应用程序。我一直在捕获用户的电子邮件,以此作为链接本地和FB帐户的方式。

突然,(在我在FB中创建一个新的测试应用程序的时候),我不再收到登录到FB的电子邮件。鉴于我使用自己的帐户进行测试,应该没有问题。这是FB战略:

passport.use(new FacebookStrategy({
    clientID: ids.facebook.clientID,
    clientSecret: ids.facebook.clientSecret,
    callbackURL: ids.facebook.callbackURL,
    enableProof: false,
    profileFields: ['id', 'displayName', 'emails']
  },
  function(accessToken, refreshToken, profile, done) {
    console.log(profile)
    User.findOne({ username: profile.emails[0].value }, function (err, user) {
      if(err) {
        return done(err)
      }
      if (user) {
        // if doesn't contain facebook id, add it
        user.update({facebookId : profile.id}, function(err, user) {
          if (err) {
            return done(err)
          }
        })
        return done(null, user)
      }
      if (! user) {
        User.create({username: profile.emails[0].value, facebookId: profile.id}, function(err, user) {
          if(err) {
            return done(err, null)
          }
          return done(null, user)
        })
      }
    });
  }
));

显示配置文件的行现在返回:

GET /auth/facebook 302 0.981 ms - 0
{ id: 'xxxxxxxxxxxxxxx',
  username: undefined,
  displayName: 'Dan Donaldson',
  name: 
   { familyName: undefined,
     givenName: undefined,
     middleName: undefined },
  gender: undefined,
  profileUrl: undefined,
  provider: 'facebook',
  _raw: '{"id":"xxxxxxxxxxxxxxx","name":"Dan Donaldson"}',
  _json: { id: 'xxxxxxxxxxxxxxx', name: 'Dan Donaldson' } }

有人见过这个问题吗? FB不再为登录主应用程序或创建的测试应用程序的用户提供电子邮件。我有一种感觉这是一个FB问题,而不是node / js / express / passport问题,因为没有任何与此关系的代码更改似乎是相关的....

1 个答案:

答案 0 :(得分:1)

根据我上面的评论:我在路线中添加了一个字段规范:

router.get("/facebook", passport.authenticate('facebook', {scope: ['email']}));

这是在文档中,但似乎有一些疑问它是必需的,并且(我认为这是相关点),代码在没有它的情况下在localhost上工作。所以我或多或少地忘记了它。

我还没有经过测试,但可能只有路线很重要,但无论如何我都会在两个地方都有,现在工作正常。