【问题标题】:nodejs passport - facebook authenticationnodejs 护照 - facebook 身份验证
【发布时间】:2016-01-19 10:06:51
【问题描述】:

我创建了一个 Facebook 应用程序,只有我自己使用。现在我已经上线了,但除了我之外没有其他人能够登录。

他们得到以下错误。

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)

这可能是什么问题。应用已上线。

【问题讨论】:

    标签: node.js facebook passport.js passport-facebook


    【解决方案1】:

    我偶然发现了这个帖子:https://github.com/jaredhanson/passport-facebook/issues/93

    普遍的共识似乎是jdomingo's solution 正在为人们工作。他的解决方案包括在策略中启用enableProof

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

    jdomingo 提供了一些 further explanation 说明为什么这会有所帮助。 FWIW,我自己并没有真正尝试过。

    【讨论】:

      【解决方案2】:

      看来你不是唯一一个:

      here

      他们提供了多种解决方案。

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

      【讨论】:

        【解决方案3】:

        我在 Express 中有一个 REST api,它使用来自 Jeroen Pelgrims 的信息使用无会话 Facebook 登录。它的作用是:

        使用 Facebook 策略登录 在用户数据库中保存令牌和信息 使用该令牌进行不记名登录 passport-facebook 身份验证在回调页面中使用的是: passport.authenticate('strategy', options, user, error)

        该行为正确地引发了该错误!正如@jsilveira 在 cmets 中所说,如果用户登录两次,就会发生错误......

        我的答案很简单,我发现了错误...继续阅读有一个但是...

        // facebook 认证和登录的路由

         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});
                }
            }
        );
        

        但是,如果你们希望它再次登录/授权,您可以在错误部分插入回调或重定向。

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

        PS:stackoverflow.com 有一个重新验证的答案,如下所示:

        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);
        });
        

        【讨论】:

          【解决方案4】:

          您需要在 FacebookStrategy 回调中调用 done()。对于测试,您可以这样做

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

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

          【讨论】:

            猜你喜欢
            • 2013-06-28
            • 2013-08-26
            • 1970-01-01
            • 1970-01-01
            • 2015-08-21
            • 2013-09-04
            • 1970-01-01
            • 2021-06-14
            • 1970-01-01
            相关资源
            最近更新 更多