TokenError: The code passed is incorrect or expired PASSPORT

TokenError: The code passed is incorrect or expired PASSPORT

您好,我的护照号码有误:

The code passed is incorrect or expired.
500

TokenError: The code passed is incorrect or expired.
    at Strategy.OAuth2Strategy.parseErrorResponse (/usr/app/node_modules/passport-oauth2/lib/strategy.js:329:12)
    at Strategy.OAuth2Strategy._createOAuthError (/usr/app/node_modules/passport-oauth2/lib/strategy.js:376:16)
    at /usr/app/node_modules/passport-oauth2/lib/strategy.js:166:45
    at /usr/app/node_modules/passport-github/lib/strategy.js:77:16
    at /usr/app/node_modules/oauth/lib/oauth2.js:209:7
    at passBackControl (/usr/app/node_modules/oauth/lib/oauth2.js:134:9)
    at IncomingMessage.<anonymous> (/usr/app/node_modules/oauth/lib/oauth2.js:157:7)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickCallback (internal/process/next_tick.js:181:9)

除我的计算机外,我在所有计算机上都有这个问题,所以我猜有一个令牌链接到我的计算机但没有链接到其他计算机。

Twitter 护照身份验证存在此问题

我的策略:

const passport = require('passport')
const TwitterStrategy = require('passport-twitter').Strategy;
const User = require('../models/User');
const keys = require('../config/keys')

passport.serializeUser((user, done) => {
    done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findById(id).then((user) => {
        done(null, user);
    });
});

passport.use(new TwitterStrategy({
    consumerKey: keys.twitter.consumerKey,
    consumerSecret: keys.twitter.consumerSecret,
    callbackURL: "http://localhost:8080/auth/twitter/callback"
},
function(accessToken, refreshToken, profile, done) {
  User.findOne({userid: profile.id}).then((currentUser) => {
    if(currentUser){
      done(null, currentUser);
    } else {
      new User({
        userid: profile.id,
        name: profile.displayName
      }).save().then((newUser) => {
        console.log('created new user: ', newUser);
        done(null, newUser);
      });
    }
  });
}));

module.exports = passport;

我的路线:

const express = require('express');
const router = express.Router();
const passportTwitter = require('../auth/twitter');
const User = require('../models/User');

/* LOGIN ROUTER */
router.get('/login', function(req, res, next) {
  res.render('login', { title: 'Please Sign In with:' });
});

/* LOGOUT ROUTER */
router.get('/logout', function(req, res){
  req.logout();
  res.redirect('/');
});

/* TWITTER ROUTER */
router.get('/twitter',
  passportTwitter.authenticate('twitter'));

router.get('/twitter/callback',
  passportTwitter.authenticate('twitter', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/users');
  });

module.exports = router;

我知道这个错误很常见,但我已经从 Whosebug 转到 github 3 天来修复它,但我没有继续前进。所以,如果您有解决方案,我将不胜感激。

如果你想自己试试,我做了一个github:https://github.com/SPierre-Eloy/Passport_twitter_api/tree/master

编辑:我减少了将问题隔离到单一策略的代码,即 twitter 的策略。

已解决。 是MongoDB出了问题,我暂时把它去掉了,是时候找到一个持久的解决方案了