nodejs passport - facebook身份验证

时间:2015-10-20 16:25:04

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

我创建了一个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)

这可能是什么问题。应用程序正在运行。

4 个答案:

答案 0 :(得分:1)

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

使用Facebook策略登录 在用户数据库中保存令牌和信息 使用该令牌进行Bearer登录 passport-facebook authenticate在回调页面中使用的是: passport.authenticate(&#39;策略&#39;,选项,用户,错误)

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

我的回答非常简单,我抓住了错误...继续阅读有一个但是......

//用于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);
});

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

我偶然发现了这个帖子: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,我其实并没有亲自尝试过这个。

答案 3 :(得分:0)

看起来你不是唯一一个:

here

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

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