Passport-facebook 没有收到电子邮件 [英] Passport-facebook doesn't get email

查看:34
本文介绍了Passport-facebook 没有收到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 express js 和passport-Facebook 在我的网站中实现了Facebook-LogIn.它运行良好(我得到了字段配置文件),但问题是我没有收到电子邮件.我收到一个错误:

I have already implemented the Facebook-LogIn in my website with express js and passport-Facebook. It works well (I get field profile), but the problem is that I don't get the Email. I get an error:

email   : profile.emails[0].value,

类型错误:无法读取未定义的属性0"

TypeError: Cannot read property '0' of undefined

我的代码:

passport.use('facebook',new FacebookStrategy({
        clientID            : config.facebook.id,
        clientSecret    : config.facebook.secret,
        callbackURL  : '/auth/facebook/callback',
        profileFields : ['id', 'displayName', 'emails','photos']
    }, function(accessToken, refreshToken, profile, done) {

        User.findOne({provider_id: profile.id}, function(err, user) {
            if(err) throw(err);
            if(!err && user!= null) return done(null, user);

            var user = new User({
                provider_id : profile.id,
                name                 : profile.displayName,
                email               : profile.emails[0].value,
                photo               : profile.photos[0].value,
            });
            user.save(function(err) {
                if(err) throw err;
                return done(null, user);
            });
        });
    }));

如果有人能帮助我解决我的问题,那就太好了:)

It would be great if someone could help me with the solution to my problem :)

推荐答案

我遇到了同样的问题.我们有 10 个测试用户,所有 10 个用户的电子邮件地址都与他们的 Facebook 帐户相关联.但是对于 10 名测试人员之一,Facebook 没有在个人资料响应中返回电子邮件"JSON 属性.我不知道为什么,因为它看起来与其他运行良好的 Facebook 个人资料相同.

I had the same problem. We had 10 test users, all 10 had email addresses associated with their Facebook account. But for one of the 10 testers, Facebook did not return the 'email' JSON property in the profile response. I have no idea why, since it looked identical to other Facebook profiles that worked fine.

修复方法是更改​​此行:

The fix was to change this line:

passport.authenticate('facebook')

为此:

passport.authenticate('facebook', { scope: [ 'email' ] })

我仍然无法解释为什么它适用于 9/10,而不适用于 9/10.无论哪种方式,它现在都已修复:-)

I still can't explain why it worked for 9/10, but not for one. Either way, it's fixed now :-)

这篇关于Passport-facebook 没有收到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆