Facebook护照策略返回500错误

我试图做一个loginlogin用户使用passport.js facebook战略连接他们的帐户与Facebook,并保存他们的profile photo, id, gender, timeline cover and token根据userSchema(在user.js模型如下所示。

我尝试了很多组合,但仍然从Facebook获得500错误,或者如果显示Facebook身份validation,Facebook无法返回(代码组合,我试过)并保存该对象。

PS我在Facebook上input了正确的callbackurl

PPS:请参阅我的更新routes.js和更新下面的passport.js

这是我的routes.js文件:

  app.get('/auth/connect/facebook', passport.authenticate('facebook-connect', { authType: 'rerequest', scope: ['id', 'cover', 'gender', 'photos'] })); app.get('/auth/connect/facebook/callback', passport.authenticate('facebook-connect', { successRedirect: '/profile/configure', failureRedirect: '/profile/congigure' // failureFlash: true })); 

我的facebook-connect的passport.js文件:

 passport.use('facebook-connect', new FacebookStrategy({ clientID: configAuth.facebookAuth.clientID, clientSecret: configAuth.facebookAuth.clientSecret, callbackURL: configAuth.facebookAuth.callbackURL, profileFields: ['id', 'cover', 'gender', 'photos'], enableProof: true }, function(token, refreshToken, profile, cb) { process.nextTick(function() { User.findOne({ 'local.facebook.id': profile.id }, function(err, user) { if (err) return cb(err); if (user) { return cb(null, false, req.flash('fbflash', 'This facebook user is already connected with an account at eBird.')); } else { user.local.facebook.id = profile.id; user.local.facebook.token = token; user.local.profile.gender = profile.gender; user.local.profile.herobg = profile.cover; user.local.profile.dp = user.local.profile.dp ? user.local.profile.dp : profile.photos[0].value; if (user.local.profile.dp == '') { if (user.local.profile.gender == 'male') { user.local.profile.dp = 'http://res.cloudinary.com/pinterested222/image/upload/v1487659283/an-av-3_jxrhwc.png'; } if (user.local.profile.gender == 'female') { user.local.profile.dp = 'http://res.cloudinary.com/pinterested222/image/upload/v1487770814/female-avatar_vvyvtj.png'; } } user.save(function(err) { if (err) throw err; return cb(null, user); }); } }); }); })); 

我的user.js模型:

 var mongoose = require('mongoose'); var bcrypt = require('bcrypt-nodejs'); var DateOnly = require('mongoose-dateonly')(mongoose); var shortid = require('shortid'); var uniqueValidator = require('mongoose-unique-validator'); var userSchema = mongoose.Schema({ _id: { type: String, default: shortid.generate }, local: { email: String, username: { type: String, unique: true }, firstname: String, surname: String, name: String, role: { type: String, default: 'user' }, department: String, pno: Number, password: String, verified: { type: Boolean, default: false }, profile: { dp: String, createdAt: { type: Date, default: Date.now }, herobg: String, location: String, website: String, gender: String, birthday: DateOnly, lastlogin: { type: Date }, notifications: { name: String, namedp: String, type: { type: String }, date: { type: Date, default: Date.now }, read: { type: Boolean, default: false } } }, facebook: { id: String, token: String } } }); userSchema.plugin(uniqueValidator, { message: '{Path}:{VALUE} is already taken.' }); userSchema.methods.generateHash = function(password) { return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); }; userSchema.methods.validPassword = function(password) { return bcrypt.compareSync(password, this.local.password); }; // userSchema.methods.bellTimesAgo = function(date); module.exports = mongoose.model('User', userSchema); 

错误,这是抛出我:

 The www.facebook.com page isn't working www.facebook.com is currently unable to handle this request. HTTP ERROR 500 

任何帮助将不胜感激,谢谢。


更新 – 1

我读 (从passportjs文档 )有关passport.authorize()和更新我的passport.js文件通过passport.authorize()和更新我的路线,但仍然是同样的问题。

这里是我更新的passport.js

 // Facebook Strategy Updated using authorize passport.use(new FacebookStrategy({ clientID: configAuth.facebookAuth.clientID, clientSecret: configAuth.facebookAuth.clientSecret, callbackURL: configAuth.facebookAuth.callbackURL, // profileFields: ['id', 'cover', 'gender', 'photos'], // enableProof: true, passReqToCallback: true }, function(req, accessToken, refreshToken, profile, done) { process.nextTick(function() { if (!req.user) { User.findOne({ 'local.facebook.id': profile.id }, function(err, user) { if (err) return done(err); if (user) { return done(null, false, req.flash('fbflash', 'This facebook user is already connected with an account at eBird.')); } else { user.local.facebook.id = profile.id; user.local.facebook.token = accessToken; user.local.profile.gender = profile.gender; user.local.profile.herobg = profile.cover; user.local.profile.dp = user.local.profile.dp ? user.local.profile.dp : profile.photos[0].value; if (user.local.profile.dp == '') { if (user.local.profile.gender == 'male') { user.local.profile.dp = 'http://res.cloudinary.com/pinterested222/image/upload/v1487659283/an-av-3_jxrhwc.png'; } if (user.local.profile.gender == 'female') { user.local.profile.dp = 'http://res.cloudinary.com/pinterested222/image/upload/v1487770814/female-avatar_vvyvtj.png'; } } user.save(function(err) { if (err) throw err; return done(null, user); }); } }); } else { var user = req.user; user.local.facebook.id = profile.id; user.local.facebook.token = accessToken; user.local.profile.gender = profile.gender; user.local.profile.herobg = profile.cover; user.local.profile.dp = user.local.profile.dp ? user.local.profile.dp : profile.photos[0].value; if (user.local.profile.dp == '') { if (user.local.profile.gender == 'male') { user.local.profile.dp = 'http://res.cloudinary.com/pinterested222/image/upload/v1487659283/an-av-3_jxrhwc.png'; } if (user.local.profile.gender == 'female') { user.local.profile.dp = 'http://res.cloudinary.com/pinterested222/image/upload/v1487770814/female-avatar_vvyvtj.png'; } } user.save(function(err) { if (err) throw err; return done(null, user); }); } }); })); 

这里是我更新的routes.js

 app.get('/auth/connect/facebook', passport.authorize('facebook', { authType: 'rerequest', scope: ['id', 'cover', 'gender', 'photos'] })); app.get('/auth/connect/facebook/callback', passport.authorize('facebook', { successRedirect: '/profile/configure', failureRedirect: '/profile/configure' // failureFlash: true }) ); 

下面是我从Facebook的应用程序callback设置的快照在这里输入图像说明

错误的快照,Facebook不停地投入: 在这里输入图像说明

       

网上收集的解决方案 "Facebook护照策略返回500错误"

Passport.js文档说:

scope选项的值是特定于提供程序的。 有关支持范围的详细信息,请参阅提供者的文档。

如果您在Facebook文档中检查了允许的权限,您将找不到'id', 'cover', 'gender', 'photos'等权限。 这些项目是一个人的公开个人资料的一部分 。

所以,你应该改变scoperoutes.js从:

scope: ['id', 'cover', 'gender', 'photos']

至:

scope: ['public_profile']

或者不指定范围,因为public_profile是默认的facebook权限。

PS我告诉你的“更新1”代码版本。

从@ anton-novik获取灵感,我修复了这个错误。

问题是在我的routes.js文件。 首先看看我上面的routes.js文件,然后按照下面的代码:

 app.get('/auth/connect/facebook', ensureLoggedIn('/login'), passport.authorize('facebook', { authType: 'rerequest' })); app.get('/auth/connect/facebook/callback', passport.authenticate('facebook', { successRedirect: '/profile', failureRedirect: '/profile/settings', failureFlash: true }) ); 

没有必要为我提出的请求的scope已经批准Facebook的每个应用程序。

然后更新我的passport.js文件看起来像这样:

// // Facebook战略

 passport.use(new FacebookStrategy({ clientID: configAuth.facebookAuth.clientID, clientSecret: configAuth.facebookAuth.clientSecret, profileFields: ['id', 'picture.type(large)', 'gender', 'cover'], callbackURL: configAuth.facebookAuth.callbackURL, passReqToCallback: true }, function(req, accessToken, refreshToken, profile, done) { process.nextTick(function() { // User is not logged in yet if (!req.user) { User.findOne({ 'local.facebook.id': profile.id }, function(err, user) { if (err) return done(err); if (user) { if (!user.facebook.token) { user.facebook.token = accessToken; user.facebook.name = profile.displayName; user.facebook.email = profile.emails[0].value; user.save(function(err) { if (err) throw err; return done(null, user); }); } return done(null, user); } else { // User should be created here // and saved to mongoose } }); } //else user is logged in and needs to be merged else { console.log(profile); //display the returned json from fb // Connect the user and save the details, since the user already exsists var user = req.user; user.local.facebook.id = profile.id; user.local.facebook.token = accessToken; user.local.profile.gender = profile.gender; user.local.profile.dp = profile.photos[0].value; user.local.profile.herobg = profile._json.cover.source; user.save(function(err) { if (err) throw err; return done(null, user); }); } }); })); 

希望它可以帮助某人。 🙂