node.js - Facebook OAuth2 不提供用户电子邮件

标签 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 服务吗?如果是这样,您会收到用户的电子邮件吗?如何? “直接”方式?解决方法?

最佳答案

passportjs 中的 Facebook 策略需要在选项中包含 profileFields 字段。尝试在选项中传递“电子邮件”。

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

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

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,已安装,感兴趣,语言,meeting_for,name_format,政治,引号,relationship_status,宗教,significant_other,体育,updated_time,网站';

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);
    });
  }
));

```

关于node.js - Facebook OAuth2 不提供用户电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19073128/

相关文章:

javascript - 无法使用 Fetch-node 存储 get 调用的结果

azure - Microsoft "converged"OAuth 授权组织 O365 帐户时出现错误 AADSTS90093

javascript - 通过socket.io发送给特定的人

node.js - req.user undefined - node + express + passport-facebook

oauth-2.0 - 使用承载 token 的MVC 5 Identity 2和Web API 2授权和调用api

node.js - Passport 序列化和反序列化与 JWT

node.js - NodeJS 生成用于签名和验证消息的有效 PEM key

javascript - ExpressJS 响应中间件

node.js - Mongoose - 可能的循环依赖?

security - 如何使用后端 golang 通过 squareup 进行身份验证?