Facebook OAuth2不提供用户电子邮件

Pon*_*oni 0 node.js oauth-2.0 facebook-oauth passport.js

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

我正在使用NodeJS,PassportJS-Facebook.

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

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

This app needs: 
Your basic info
Your email address (xyz@example.com)
Run Code Online (Sandbox Code Playgroud)

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

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

Eug*_*ace 6

在visajs的Facebook战略,期望profileFields在选项中的一个领域.尝试在选项中传递"电子邮件".

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

另外,您也可以覆盖profileUrloptions并发送:

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);
    });
  }
));
Run Code Online (Sandbox Code Playgroud)

```