Node.js Can';无法从Facebook passport获取电子邮件信息
目前,我正在进行一个项目来存储来自Facebook身份验证的信息,我已经检查了该解决方案,这是我的代码Node.js Can';无法从Facebook passport获取电子邮件信息,node.js,passport.js,passport-facebook,Node.js,Passport.js,Passport Facebook,目前,我正在进行一个项目来存储来自Facebook身份验证的信息,我已经检查了该解决方案,这是我的代码 passport.use(new FacebookStrategy( { clientID: 'xxxxxxxxxxxxx', clientSecret: 'xxxxxxxxxxxxxxxx', callbackURL: 'http://1bc600b4.ngrok.io/auth/facebook/callback', profileFields: ['i
passport.use(new FacebookStrategy(
{
clientID: 'xxxxxxxxxxxxx',
clientSecret: 'xxxxxxxxxxxxxxxx',
callbackURL: 'http://1bc600b4.ngrok.io/auth/facebook/callback',
profileFields: ['id', 'displayName', 'name', 'photos', 'email'],
},
(accessToken, refreshToken, profile, done) => {
console.log(profile);
models.User.findOne({ where: { facebookID: profile.id } }).then((existinguser) => {
if (existinguser) {
// Nothing will happen, the ID already exists
done(null, existinguser);
} else {
models.User.create({
// email: profile.emails[0].value,
username: 'hello',
firstname: profile.name.givenName,
lastname: profile.name.familyName,
facebookID: profile.id,
avatar: `https://graph.facebook.com/${profile.id}/picture?redirect=true&height=470&width=470`,
avatarThumb: profile.photos[0].value,
last_login: Date.now(), }).then(user => done(null, user));
}
});
},
));
app.use(passport.initialize());
app.get('/flogin', passport.authenticate(
'facebook',
passport.authenticate('facebook', { scope: ['profile', 'email'] }),
));
app.get(
'/auth/facebook/callback',
passport.authenticate('facebook', { session: false }),
(req, res) => {
res.send('AUTH WAS GOOD!');
},
);
我不明白为什么我用这种方式,电子邮件信息仍然没有显示,这使我失去了很多信息,有谁能给我一个提示来解决这个问题吗?谢谢大家! 这个问题已经存在一段时间了,您需要包括电子邮件范围,即使这样,由于用户隐私设置或用户的电子邮件未在facebook端验证,它也可能不会发送facebook电子邮件 您可以这样添加作用域:
passport.use(
new FacebookStrategy({
clientID: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
callbackURL: "http://www.example.com/auth/facebook/callback"
passReqToCallback : true,
profileFields: ['emails'] // email should be in the scope.
})
)
1. profile.emails // [email1@example.com, somerandomdude@gmail.com]
2. profile.emails[1].value // randomdude@yahoo.com
username@facebook.com // this exists.
您还需要将其添加到授权路线中
app.get('/login/facebook', passport.authorize('facebook', { scope : ['email'] }));
如果电子邮件返回,它将是一个列表,您可以这样访问它们:
passport.use(
new FacebookStrategy({
clientID: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
callbackURL: "http://www.example.com/auth/facebook/callback"
passReqToCallback : true,
profileFields: ['emails'] // email should be in the scope.
})
)
1. profile.emails // [email1@example.com, somerandomdude@gmail.com]
2. profile.emails[1].value // randomdude@yahoo.com
username@facebook.com // this exists.
如果没有返回,您可以使用他们的用户名或id,至少暂时创建一封facebook电子邮件,如下所示:
passport.use(
new FacebookStrategy({
clientID: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET',
callbackURL: "http://www.example.com/auth/facebook/callback"
passReqToCallback : true,
profileFields: ['emails'] // email should be in the scope.
})
)
1. profile.emails // [email1@example.com, somerandomdude@gmail.com]
2. profile.emails[1].value // randomdude@yahoo.com
username@facebook.com // this exists.
嗨,谢谢你的回答,我试了好几次,发现这些代码对我有效,我发现我不需要将passReqToCallback添加到我的代码中,否则它会一直显示done不是一个函数很棒,很高兴知道它解决了你的问题!你是最棒的!