nodejs护照 – 脸书authentication

我创build了一个Facebook应用程序,只有我使用。 现在我已经成功了,但是除了我以外没有其他人能够login。

他们得到以下错误。

FacebookTokenError: This authorization code has been used. at Strategy.parseErrorResponse (/var/www/html/project/node_modules/passport-facebook/lib/strategy.js:199:12) at Strategy.OAuth2Strategy._createOAuthError (/var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/lib/strategy.js:345:16) at /var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/lib/strategy.js:171:43 at /var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:177:18 at passBackControl (/var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:123:9) at IncomingMessage.<anonymous> (/var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:143:7) at IncomingMessage.EventEmitter.emit (events.js:117:20) at _stream_readable.js:920:16 at process._tickCallback (node.js:415:13) 

这里可能是什么问题。 应用程序是活的。

我在Express中有一个REST api,它使用Jeroen Pelgrims的信息来使用Sessionless Facebooklogin。 它所做的是:

用Facebooklogin策略在用户数据库中保存令牌和信息使用该令牌进行载体login什么passport-Facebookvalidation在callback页面中使用的是:passport.authenticate('strategy',options,user,error)

行为正确地抛出该错误! 正如@jsilveira在评论中所说,如果用户login两次,就会发生错误。

我的答案很简单,我发现错误…阅读有一个,但…

//用于Facebook身份validation和login的路由

  router.get('/auth/facebook', passport.authenticate('facebook', {session: false, scope : ['email'] }) ); // handle the callback after facebook has authenticated the user router.get('/auth/facebook/callback', passport.authenticate('facebook', { session: false, failureRedirect : '/'}), // on succes function(req,res) { // return the token or you would wish otherwise give eg. a succes message res.render('json', {data: JSON.stringify(req.user.access_token)}); }, // on error; likely to be something FacebookTokenError token invalid or already used token, // these errors occur when the user logs in twice with the same token function(err,req,res,next) { // You could put your own behavior in here, fx: you could force auth again... // res.redirect('/auth/facebook/'); if(err) { res.status(400); res.render('error', {message: err.message}); } } ); 

但是,如果你们想让它再次login/授权,你可以在错误部分插入一个callback或redirect。

希望这个澄清你们有一些问题。 如果你有任何问题或要求,不要害羞。 信息在我的个人资料上。

PS:stackoverflow.com有一个答案重新authentication它如下所示:

 app.post('/login', function(req, res, next) { passport.authenticate('local', function(err, user, info) { if (err) { return next(err); // will generate a 500 error } // Generate a JSON response reflecting authentication status if (! user) { return res.send({ success : false, message : 'authentication failed' }); } return res.send({ success : true, message : 'authentication succeeded' }); })(req, res, next); }); 

您需要在FacebookStrategycallback中调用done()。 为了testing,你可以做

 function(accessToken, refreshToken, profile, done) { console.log("Auth done"); done(null, profile); } 

你可以参考这里: https : //github.com/jaredhanson/passport/issues/108

我偶然发现这个线程: https : //github.com/jaredhanson/passport-facebook/issues/93

普遍的共识似乎是jdomingo的解决scheme正在为人们工作。 他的解决scheme涉及在策略中启用enableProof

 passport.use(new FacebookStrategy({ clientID: ---, clientSecret: ---, callbackURL: "http://---/auth/facebook/callback", enableProof: true } 

jdomingo提供了一些进一步的解释 ,为什么这有助于。 FWIW,我自己实际上没有尝试过。

看起来你不是唯一的一个:

看这里

他们提供几种解决scheme

无论如何 – 如果你真的想要一个解决scheme,你将不得不在这里粘贴你的代码。