调用回调 URL 时获取"Token Error: Bad Request"



每当我使用google-Oauth2调用回调URL时,我都会得到以下内容。

错误追溯:

TokenError: Bad Request
at Strategy.OAuth2Strategy.parseErrorResponse (G:projectsoauthnode_modulespassport-oauth2libstrategy.js:358:12)
at Strategy.OAuth2Strategy._createOAuthError (G:projectsoauthnode_modulespassport-oauth2libstrategy.js:405:16)
at G:projectsoauthnode_modulespassport-oauth2libstrategy.js:175:45
at G:projectsoauthnode_modulesoauthliboauth2.js:191:18
at passBackControl (G:projectsoauthnode_modulesoauthliboauth2.js:132:9)
at IncomingMessage.<anonymous> (G:projectsoauthnode_modulesoauthliboauth2.js:157:7)
at IncomingMessage.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

谷歌战略:

passport.use(
new GoogleStrategy({
//options for the gooogle strategy
clientID: keys.google.clientID,
clientSecret: keys.google.clientSecret,
callbackURL:"http://localhost:3000/auth/google/redirect",
},(accessToken,refreshToken,profile,done) => {
// Check if user exist in our database
User.findOne({googleId: profile.id}).then((currentUser)=>{
if(currentUser){
// already have the user
console.log('user is ', currentUser);
done(null,currrentUser);
}else{
// if not create a user in db
new User({
username: profile.displayName,
googleId: profile.id 
}).save().then((newUser) =>{
console.log('new user created' , newUser);
done(null,newUser);
});
}
})

})
);

回叫路线:

//auth with google 
router.get('/google',passport.authenticate('google',{
scope:['profile']
}));
//callback for google to redirect to
router.get('/google/redirect',passport.authenticate('google'),(req,res) =>{
//res.send(req.user);
res.redirect('/profile');
});

我是这方面的新手。有人能告诉我为什么会出现这个错误吗?

u必须使用passport.serializeUser并调用其中的done方法,还使用passport.initialize方法作为中间件在您使用passport.authenticate的路由上也遇到了同样的问题

相关内容

最新更新