简体   繁体   English

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

[英]Not getting back email from Facebook OAuth API, Passport with MEAN Stack

I'm working to set up Face book OAuth for user logging into my website. 我正在努力为用户登录我的网站设置Face book OAuth。 I am getting an object back from Facebook with my FB information but there is no sign of email anywhere in that profile response. 我从Facebook获得了一个带有我的FB信息的对象,但在该个人资料回复中没有任何电子邮件的迹象。

When I click the "Login to Facebook" button on my page it takes me to Facebook and says the app will require my profile and email as I added email to the scope of things I wanted. 当我点击我页面上的“登录Facebook”按钮时,它将我带到Facebook并说该应用程序将需要我的个人资料和电子邮件,因为我添加了电子邮件到我想要的范围。 Here is my code: 这是我的代码:

config/routes.js: 配置/ 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.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);
                            }
                        });
                    }
             });
        });
}));

Here is the FB object I get back when I console.log(profile) in the process.nextTick function with FB's response: 这是我在使用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' } }

I made sure my email is public and login to Facebook normally with my email and password, so I'm not using Mobile Phone to sign to Facebook. 我确保我的电子邮件是公开的,并且通常使用我的电子邮件和密码登录到Facebook,所以我没有使用手机登录Facebook。 I'm just missing that email address. 我只是错过了那个电子邮件地址。 I can't seem to figure out why that object isn't giving me back an email?? 我似乎无法弄清楚为什么那个对象没有给我回电子邮件? What am I missing? 我错过了什么? Help is very much appreciated. 非常感谢帮助。

I find another solution for this issue with PASSPORT-FACEBOOK node module. 我找到了PASSPORT-FACEBOOK节点模块的另一个解决方案。 Hope this will help others. 希望这会有助于他人。

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

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

Search for "Declarative Fields" in the changelog: https://developers.facebook.com/docs/apps/changelog#v2_4 在更改日志中搜索“声明字段”: https//developers.facebook.com/docs/apps/changelog#v2_4

You now have to specify the fields you want to get in the API call: /me?fields=name,email 您现在必须在API调用中指定要获取的字段: /me?fields=name,email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM