Facebook OAuth2不提供用户电子邮件

时间:2013-09-29 00:22:28

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

这已被多次询问,但似乎没有已知的解决办法,所以我发布这个问题,希望有人确实有解决方法。

我正在使用NodeJS,PassportJS-Facebook。

app.get("/auth/facebook",
            passport.authenticate("facebook", {
                scope : [ "email" ]
            }),
            function (req, res) {
            });

起初我认为这是一个PassportJS问题,但我当然已经取消了这个选项 我正在使用的Facebook用户帐户明确指出:

This app needs: 
Your basic info
Your email address (xyz@example.com)

这个已知问题的一些链接(尚未解决!): https://developers.facebook.com/bugs/298946933534016 https://developers.facebook.com/bugs/429653750464521 https://developers.facebook.com/bugs/482815835078469

那么,您使用Facebook的OAuth服务吗?如果是这样,你收到用户的电子邮件吗?怎么样? “直”的方式?解决方法?

2 个答案:

答案 0 :(得分:6)

在passportjs中的Facebook策略,期望选项中有profileFields字段。尝试在选项中传递“电子邮件”。

strategyOptions.profileFields = ['emails', 'first_name', 'last_name'];

或者,您可以覆盖profileUrl中的options并发送:

strategyOptions.profileURL = 'https://graph.facebook.com/me?fields=location,first_name,last_name,middle_name,name,link,username,work,education,gender,timezone,locale,verified,picture,about,address,age_range,bio,birthday,cover,currency,devices,email,favorite_athletes,id,hometown,favorite_teams,inspirational_people,install_type,installed,interested_in,languages,meeting_for,name_format,political,quotes,relationship_status,religion,significant_other,sports,updated_time,website';

Facebook会忽略您没有权限的字段(如电子邮件)。

这应该在这里:

passport.use(new FacebookStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: "http://localhost:3000/auth/facebook/callback",
    profileUrl: "  ..... ",
    //or
    profileFields: [ ... ];
  },
  function(accessToken, refreshToken, profile, done) {
    // asynchronous verification, for effect...
    process.nextTick(function () {

      // To keep the example simple, the user's Facebook profile is returned to
      // represent the logged-in user.  In a typical application, you would want
      // to associate the Facebook account with a user record in your database,
      // and return that user instead.
      return done(null, profile);
    });
  }
));

```

答案 1 :(得分:0)

您必须在设置对象中提供字段“范围”:

new FacebookStrategy({
    clientID: FACEBOOK_APP_ID,
    clientSecret: FACEBOOK_APP_SECRET,
    callbackURL: "http://localhost:3000/auth/facebook/callback",
    profileUrl: "  ..... ",
    scope: "email",
    //or
    profileFields: [ ... ];
  }

尝试查看来源。