javascript - 通过的代码不正确或护照过期
问题描述
我不断得到
传递的代码不正确或已过期。
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');
});
解决方案
由本次回购记入
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');
});
推荐阅读
- python - 学位、亲近和等级声望
- talend - 更新过程中出现错误,这通常是由 TAC 中的错误更新配置引起的
- java - 用Java上传excel文件时如何修复错误415不支持的媒体类型错误?
- android - 即使在正确响应之后,Retrofit 的 onFailure 方法也会被调用
- powershell - PowerShell 5.1 设置 Acl
- reactjs - 反应组件中显示的陈旧数据
- android - getSupportFragmentManager.popBackStackImmediate(tag,FragmentManager.POP_BACK_STACK_INCLUSIVE) 弹出所有片段
- mysql - 根据另一列将一列聚合到不同的字段
- java - 如何避免 Java 中的代码重复
- python - 如何使用 Python 在 MySQL 中将列表作为列插入?