gpt4 book ai didi

javascript - Node.js 上的 TokenError : Bad Request; Google OAuth2; Passport. js;能够 console.log 数据,但是会出现错误

转载 作者:行者123 更新时间:2023-12-01 15:46:03 27 4
gpt4 key购买 nike

我正在尝试使用 Passport.js授权谷歌OAuth2Node.js .我整个星期都在尝试让它工作,但不知道为什么它不工作,所以现在我求助于 stack 寻求一些潜在的帮助。我已经尝试了所有在线论坛上提供的类似问题的解决方案。

每次发送请求时,它都会返回 TokenError: Bad Request,但是,它能够 console.log 所需的数据,所以这对我来说表明 token 实际上是成功的。我无法解释为什么会发生这种情况。

我尝试过更具体的回调请求,例如 http://localhost:3000/auth/google/redirect .
我已经尝试过所有其他类型的 Oauth 类型,谷歌有 Node 服务器、Web 应用程序、html 等。
我尝试过不同的端口。

授权路线

 const router = require('express').Router();
const passport = require('passport');

// auth login
router.get('/login', (req, res) => {
res.render('login', { user: req.user });
});

// auth logout
router.get('/logout', (req, res) => {
// handle with passport
res.send('logging out');
});

// auth with google+
router.get('/google', passport.authenticate('google', {
scope: ['profile']
}));

// callback route for google to redirect to
// hand control to passport to use code to grab profile info
router.get('/google/redirect', passport.authenticate('google'),
(req,
res) => {
res.send('you reached the redirect URI');
});

module.exports = router;

PASSPORT_SETUP
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const keys = require('./keys');

passport.use(
new GoogleStrategy({
// options for google strategy
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret,
callbackURL: '/auth/google/redirect'
}, (accessToken, refreshToken, profile, done) => {
// passport callback function
console.log('passport callback function fired:');
console.log(profile);
})
);

当提交过程通过登录页面进行时,将所需的结果传递到 console.log,然后等待 localhost 大约 1 分钟。

如您所见,它试图检索的内容已经在控制台中。

console

然后它继续抛出和错误:

Error

最佳答案

抱歉回复晚了,挖了一些旧代码,这是它被标记为“所有身份验证方法正常工作”的地方。正如 Aritra Chakraborty 在评论中所说,没有调用“完成”方法。请参阅 Nedb 的以下实现。

const GoogleStrategy = require('passport-google-oauth20').Strategy;
const Datastore = require('nedb');
const database = new Datastore('database.db');
database.loadDatabase();

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

passport.deserializeUser((googleId, done) => {
database.findOne({ googleId : googleId }, (err, user) => {
done(null, user);
});
});

var strategy = new GoogleStrategy({
// options for google strategy
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret,
callbackURL: '/auth/google/redirect'
}, (accessToken, refreshToken, object0, profile, done) => {
// check if user already exists in our own db
database.findOne({ googleId: profile.id }, (err, currentUser) => {
if (currentUser !== null) {
done(null, currentUser);
} else {
var d = new Date();
var n = d.getTime();
var duoID = uuidv1();
var User = {
duoVocalID: duoID,
googleId: profile.id,
username: profile.displayName,
thumbnail: profile._json.image.url,
oscope: object0.scope,
oaccess_token: object0.access_token,
otoken_type: object0.token_type,
oid_token: object0.id_token,
oexpires_in: object0.expires_in,
oemails: profile.emails,
olanguage: profile._json.language,
oname: profile.name,
TimeOfLastLogon: n,
RefreshToken: refreshToken
};

database.insert(User, (err, newUser) => { });
var newUser = User;
done(null, newUser);
}
});
});

passport.use(strategy);

// auth with google+
app.get('/auth/google', passport.authenticate('google', {
scope: ['profile', 'email', 'https://www.googleapis.com/auth/spreadsheets'],
accessType: 'offline',
approvalPrompt: 'force'
}));

// callback route for google to redirect to
// hand control to passport to use code to grab profile info
app.get('/auth/google/redirect', passport.authenticate('google'), async (req, res) => {
var userString = JSON.stringify(req.user)
jwt.sign({userString}, 'secretKey', { expiresIn: '365d' }, (err, token) => {
res.send("<script>localStorage.setItem('token', '"+token+"'); window.close(); window.opener.document.getElementById('modal-toggle').checked = false;</script>");
});
});

关于javascript - Node.js 上的 TokenError : Bad Request; Google OAuth2; Passport. js;能够 console.log 数据,但是会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57482861/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com