我正在使用 Meanjs.org 样板文件,Facebook 注册会将我返回到注册页面。 以下是我到目前为止所采取的步骤。
1) 设置 Facebook 应用程序 站点 URL
以及OAuth的回调URI
2) 将 APP_ID 和 APP_Secret 放入 Client_ID 和 Client_Secret
facebook: {
clientID: process.env.FACEBOOK_ID || '*****',
clientSecret: process.env.FACEBOOK_SECRET || '*****',
callbackURL: 'http://localhost:3000/auth/facebook/callback',
profileFields: ['id','emails', 'first_name', 'last_name', 'displayName', 'link', 'about_me', 'photos' ]
},
3)代码如下
--路线
// Setting the facebook oauth routes
app.route('/auth/facebook').get(passport.authenticate('facebook', {
scope: ['email']
}));
app.route('/auth/facebook/callback').get(users.oauthCallback('facebook'));
-- oauthCallback 函数,
exports.oauthCallback = function(strategy) {
return function(req, res, next) {
passport.authenticate(strategy, function(err, user, redirectURL) {
if (err || !user) {
console.log('1' + err);
//console.log(user);
return res.redirect('/#!/signin');
}
req.login(user, function(err) {
if (err) {
console.log('2' + err);
return res.redirect('/#!/signin');
}
return res.redirect(redirectURL || '/');
});
})(req, res, next);
};
};
-- Passport-Facebook 策略
module.exports = function() {
// Use facebook strategy
passport.use(new FacebookStrategy({
clientID: config.facebook.clientID,
clientSecret: config.facebook.clientSecret,
callbackURL: config.facebook.callbackURL,
passReqToCallback: true
},
function(req, accessToken, refreshToken, profile, done) {
console.log('facebook Strategy Started');
// Set the provider data and include tokens
var providerData = profile._json;
providerData.accessToken = accessToken;
providerData.refreshToken = refreshToken;
// console.log(JSON.stringify(profile));
console.log(profile);
// console.log(JSON.stringify(profile.name.givenName));
// Create the user OAuth profile
var providerUserProfile = {
firstName: profile.name.givenName,
lastName: profile.name.familyName,
displayName: profile.displayName,
email: profile.emails[0].value,
username: profile.username,
provider: 'facebook',
providerIdentifierField: 'id',
providerData: providerData
};
//console.log('provider' + providerUserProfile);
// Save the user OAuth profile
users.saveOAuthUserProfile(req, providerUserProfile, done);
}
));
};
4) 调试
在 oauthCallback 函数下记录错误返回以下内容,
1TypeError:无法读取未定义的属性“0”
Facebook 在 Passport-Facebook 模块中返回的个人资料如下,
{ id: 'Id_of_the_person', 用户名:未定义, displayName: '人物全名', 姓名: { 家庭名称:未定义, 给定名称:未定义, 中间名:未定义}, 性别:未定义, 配置文件网址:未定义, 提供商:“脸书”, _raw: '{"name":"Full_name_of_person","id":"Id_of_the_person"}', _json: { name: 'Id_of_the_person', id: 'Id_of_the_person', accessToken: 'access_token_value', 刷新令牌:未定义}}
任何人都可以指导我从 Facebook 获取正确的用户个人资料(包括用户电子邮件)吗?
非常感谢。
我的个人资料字段设置为以下
profileFields: ['email','id', 'first_name', 'gender', 'last_name', 'picture']
即使您设置了电子邮件,如果用户有多个电子邮件,它也可能会返回电子邮件。因此,您需要检查电子邮件是否返回 profile.email 或 profile.emails[0].value。您还必须检查它是否未定义,因为有些人在 Facebook 注册时从未验证他们的电子邮件帐户,而有些人则使用电话号码注册,在这两种情况下,他们的电子邮件将始终是未定义的。
您想要检查所有必填字段是否有值。
var email = profile.email || profile.emails[0].value;
if (! email){
console.log('this user has no email in his FB');
var err = {message:'this user is missing an email'};
return done(err);
}
现在如果他们有电子邮件我就可以这样做
newUser.facebook.email = email;
如果他们没有电子邮件,您可以设置个人资料会话并将他们发送到要求他们输入电子邮件的页面。
这听起来很痛苦,但你永远不能相信来自第三方 api 的信息具有值。
我在网上看到的大多数护照示例都是错误的。他们都假设存在电子邮件。
谢谢所有这些答案,我遇到了同样的问题,但我已经用这种方式解决了它