【问题标题】:The code passed is incorrect or expired in passport通过的代码不正确或护照过期
【发布时间】:2019-02-11 21:12:04
【问题描述】:

我一直在努力

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

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。

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

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

        }
      });
    }
  ));
};

routes/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');
});

【问题讨论】:

  • 发布完整代码 - auth/github/callbackauth/github 的路由处理程序
  • 好的,我现在更新了。
  • 嗯没有什么不寻常的。仅供参考,“代码”是 oauth 流在参数中传递回您的回调路由的东西,然后您应该向它发出请求以获取 accessToken 等。奇怪的是它说您传递的是不正确的。您可以首先在您的 node_modules 中添加控制台日志并确保它至少接收到代码 - github.com/jaredhanson/passport-github/blob/master/lib/…
  • yeahhhh,我将如何使用 accessToken ?
  • 我修好了。我刚刚使用护照 0auth 从另一个 github 复制。

标签: javascript express


【解决方案1】:

由这个 repo 提供

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

config/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');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-20
    • 2019-12-22
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 2015-03-16
    相关资源
    最近更新 更多