首页 > 解决方案 > 通过的代码不正确或护照过期

问题描述

我不断得到

传递的代码不正确或已过期。

GET /api/users/auth/github/callback?code=afefcf8b12561c910798 - - ms - - [0] undefined [0] TokenError: 传递的代码不正确或过期。[0] 在 Strategy.OAuth2Strategy.parseErrorResponse (/Users/eli/nodework/sequelize-demo/node_modules/passport-oauth2/lib/strategy.js:329:12) [0] 在 Strategy.OAuth2Strategy._createOAuthError (/Users/ eli/nodework/sequelize-demo/node_modules/passport-oauth2/lib/strategy.js:376:16)

我想我已经正确设置了 passport-github 0auth。

我重置了所有令牌,但仍然出现错误。

配置/passport-config.js

const GitHubStrategy = require('passport-github').Strategy;
const models = require( '../models/index');
require('dotenv').config();


module.exports = function(passport) {
  passport.serializeUser(function(user, done) {
    done(null, user.id);
  });

  // from the user id, figure out who the user is...
  passport.deserializeUser(function(userId, done){
    models.User
      .find({ where: { id: userId } })
      .then(function(user){
        done(null, user);
      }).catch(function(err){
        done(err, null);
      });
  });


  passport.use(new GitHubStrategy({
      clientID: process.env.clientID,
      clientSecret: process.env.secret,
      // if the callback is set to 5000 the 0auth app will not work for some reason
      callbackURL: 'http://127.0.0.1:5000/api/users/auth/github/callback'

    },
    function(accessToken, refreshToken, profile, cb) {
      models.User.findOne({ where: {'id': profile.id } }, 

      function (err, user) {
        if(err) {
          console.log(err);  // handle errors!
        }
        if (!err && user !== null) {
          done(null, user);
        } else {
          models.User.create({
            id: profile.id,
            username: profile.displayName,
            createdAt: Date.now()

          }).then(user => {
            console.log( refreshToken );
            console.log('user created');
            return done(null, user);
          });

        }
      });
    }
  ));
};

路线/users.js

router.get('/auth/github', passport.authenticate('github') );

router.get('/auth/github/callback', 
  passport.authenticate('github', { failureRedirect: '/' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/dashboard');

    console.log('this works');
});

标签: javascriptexpress

解决方案


由本次回购记入

https://github.com/vittau/todoapp/blob/master/server.js

配置/passport-github.js

  passport.use(new GitHubStrategy({
      clientID: process.env.clientID,
      clientSecret: process.env.secret,
      // if the callback is set to 5000 the 0auth app will not work for some reason
      callbackURL: 'http://127.0.0.1:5000/api/users/auth/github/callback'

    },
    function(accessToken, refreshToken, profile, cb) {

      models.User
      .findOrCreate({where: {id: profile.id}, defaults: {username: profile.displayName}})
      .spread(function(user, created) {
        cb(null, user)
      });

router.get('/auth/github', passport.authenticate('github', { session: false, scope: ['profile'] }) );

router.get('/auth/github/callback', 
  passport.authenticate('github', { failureRedirect: '/' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('http://127.0.0.1:3000/dashboard');

    console.log('this works');
});

推荐阅读