【问题标题】:Specific user info from facebook using passport-facebook使用 passport-facebook 来自 facebook 的特定用户信息
【发布时间】:2016-08-22 07:20:15
【问题描述】:

我正在使用 passport-facebook 进行 OAuth 并从 facebook 获取用户信息。但我得到的只是一些基本信息(即名字、姓氏)。我想要 userEmail 和friendList 之类的信息。

我目前的 Facebook 策略是 -

passport.use(new FacebookStrategy({
      clientID: configAuth.facebookAuth.appID,
      clientSecret: configAuth.facebookAuth.appSecret,
      callbackURL: configAuth.facebookAuth.callbackUrl
    },
    function(accessToken, refreshToken, profile, done) {
      process.nextTick(function() {
        User.findOne({'facebook.id': profile.id}, function(err, user) {
          if(err)
            return done(err);
          if(user)
            return done(null, user);
          else {
            var newUser = new User;
            console.log(profile);
            newUser.facebook.id = profile.id;
            newUser.facebook.token = accessToken;
            newUser.facebook.name = profile. displayName;
            newUser.save(function(err) {
              if(err)
                throw err;
              return done(null, newUser);
            })
          }
        })
      })
    }
  ));

谁能告诉我如何获取用户的电子邮件或好友列表。 提前致谢!!

【问题讨论】:

    标签: node.js authentication passport-facebook


    【解决方案1】:

    如果您需要电子邮件和 user_friends,则必须在请求 facebook-auth 时将它们添加到范围。我想添加以下代码对你有帮助:

    app.get('/auth/facebook', passport.authenticate('facebook', { scope : ['email,user_friends'] }));
    

    【讨论】:

    • 感谢您的回答。我为我工作了一半。当我看到身份验证请求时,有人说我的应用正在请求电子邮件和好友列表权限。但我得到的是朋友列表,而不是电子邮件。那么你能告诉我如何才能收到电子邮件吗?
    • 作为friend_list,email也带有一个数组。我认为您必须这样做 - var email = profile.emails[0].value; 才能收到电子邮件。如果需要var email = profile.emails;,您还可以将所有添加到帐户的电子邮件作为数组获取。
    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多