没有收到来自Facebook OAuth API的电子邮件,带有MEAN Stack的Passport

时间:2015-07-26 19:03:39

标签: javascript passport.js mean-stack facebook-oauth facebook-authentication

我正在设置Face book OAuth,以便用户登录我的网站。我从Facebook获得了一个带有我的FB信息的对象,但在该个人资料回复中没有任何电子邮件的迹象。

当我点击我页面上的“登录Facebook”按钮时,它会将我带到Facebook并说应用程序将需要我的个人资料和电子邮件,因为我添加了电子邮件到我想要的范围。这是我的代码:

配置/ routes.js:

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

app.get('/auth/facebook/callback',
  passport.authenticate('facebook', 
{ successRedirect: '/confirm_community',
  failureRedirect: '/signin' }));

============== passport.js

 passport.use(new FacebookStrategy({
    // pull in our app id and secret from our auth.js file
    clientID        : configAuth.facebookAuth.clientID,
    clientSecret    : configAuth.facebookAuth.clientSecret,
    callbackURL     : configAuth.facebookAuth.callbackURL

},

function(accessToken, refreshToken, profile, done) {
     process.nextTick(function() {
            console.log(profile);
            // console.log(profile.emails[0].value);
             User.findOne({'facebook.id':profile.id}, function (error, result){
                    if(error){
                        return done(error);
                    }
                    if(user) {
                        return done(null, user);
                    }
                    else {
                        var newUser = new User();
                        newUser.facebook.id = profile.id;
                        newUser.facebook.token = accessToken;
                        newUser.facebook.name = profile.displayName;
                        newUser.facebook.email = profile.emails[0].value;

                        newUser.save(function (error){
                            if(error){
                                console.log(error);
                                throw error;
                            } else {
                                return done(null, newUser);
                            }
                        });
                    }
             });
        });
}));

这是我在使用FB响应的process.nextTick函数中的console.log(profile)时得到的FB对象:

{ id: 'my id is coming back here',
  username: undefined, (because I don't have one)
  displayName: 'my name is coming back here',
  name:
   { familyName: undefined, (I don't have any of these in my profile)
     givenName: undefined,
     middleName: undefined },
     gender: undefined,
  profileUrl: undefined,
  provider: 'facebook',
  _raw: '{"name":"my name is coming here","id":"my id is coming here"}',
  _json: { name: 'my name is coming here', id: 'my id is coming here' } }

我确保我的电子邮件是公开的,并且通常使用我的电子邮件和密码登录到Facebook,所以我没有使用手机登录Facebook。我只是错过了那个电子邮件地址。我似乎无法弄清楚为什么那个对象没有给我回电子邮件?我错过了什么?非常感谢帮助。

2 个答案:

答案 0 :(得分:2)

我在PASSPORT-FACEBOOK节点模块中找到了解决此问题的另一种解决方案。希望这会有助于他人。

passport.use(new FacebookStrategy({
  clientID: FACEBOOK_APP_ID,
  clientSecret: FACEBOOK_APP_SECRET,
  callbackURL: "http://localhost:3000/auth/facebook/callback",
  profileFields: ['email']

参考:https://github.com/jaredhanson/passport-facebook/issues/129

答案 1 :(得分:1)

在更改日志中搜索“声明字段”:https://developers.facebook.com/docs/apps/changelog#v2_4

您现在必须在API调用中指定要获取的字段:/me?fields=name,email