[英]TokenError: Bad Request; Google OAuth2; Passport.js on Node.js; Able to console.log data, however delivers error
I am attempting to use Passport.js
to authorize Google OAuth2
on Node.js
.我正在尝试使用
Passport.js
在Node.js
上授权 Google OAuth2
。 I have tried all week to make it work and have no idea why it isn't, so am now resorting to stack for some potential help.我整个星期都在尝试让它工作,但不知道为什么它不工作,所以我现在求助于堆栈以获得一些潜在的帮助。 I have tried all solutions to similar problems available on forums online.
我已经尝试了在线论坛上提供的类似问题的所有解决方案。
Each time it sends the request it returns TokenError: Bad Request, however, it is able to console.log the required data, so this to me demonstrates that the token was in fact successful.每次发送请求时,它都会返回 TokenError: Bad Request,但是,它能够通过 console.log 记录所需的数据,所以这对我来说表明令牌实际上是成功的。 I cannot explain why this is occurring.
我无法解释为什么会发生这种情况。
I have tried being more specific in callback request eg http://localhost:3000/auth/google/redirect .我尝试在回调请求中更具体,例如http://localhost:3000/auth/google/redirect 。 I have tried every other type of Oauth type google has Node server, web application, html ect.
我已经尝试了所有其他类型的 Oauth 类型谷歌有节点服务器、Web 应用程序、html 等。 I have tried different ports.
我尝试了不同的端口。
AUTH ROUTES授权路线
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 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);
})
);
When submitted the process progresses through SignIn page, delivers desired result the console.log and then just sits for about 1 minute awaiting localhost.当提交过程通过登录页面进行时,将期望的结果传递到 console.log,然后等待 localhost 大约 1 分钟。
As you can see the very thing it is trying to retrieve is already in the console.正如你所看到的,它试图检索的东西已经在控制台中了。
It then progresses to throw and Error:然后它继续抛出和错误:
Sorry for the late reply, dug up some old code this is the point where it was marked as 'All auth methods functioning'.抱歉回复晚了,挖出一些旧代码,这是它被标记为“所有身份验证方法正常运行”的地方。 As stated by Aritra Chakraborty in the comments, "done" method was not being called.
正如 Aritra Chakraborty 在评论中所述,没有调用“完成”方法。 See the following implementation with Nedb.
请参阅以下 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}, function (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 }, function (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, function(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>");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.