我以为我正确地遵循了所有文档来实现这一点,但是,我收到了一个TokenError: Bad Request
错误消息invalid_grant
。
我的服务器非常简单:
require('dotenv').config();
import createServer from './createServer';
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
const server = createServer();
passport.use(
new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: 'http://localhost:4000/auth/callback',
},
(accessToken, refreshToken, profile, cb) => {
console.log(accessToken);
console.log(refreshToken);
console.log(profile);
cb(null, profile);
},
),
);
server.express.use(
'/auth',
passport.authenticate('google', {
scope: ['email', 'profile'],
session: false,
}),
);
server.express.use(
'/auth/callback',
passport.authenticate('google', {
successRedirect: 'http://localhost:3000/authenticate',
failureRedirect: 'http://localhost:3000/authenticate',
}),
);
server.start(
{
cors: {
credentials: true,
origin: 'http://localhost:3000',
},
},
() => console.log('Server is running on http://localhost:4000'),
);
这是我在云平台中设置 Google 的方式有问题吗?我不知道我哪里出错了。我的回调设置正确。我不知道在哪里寻找错误?
另一件令人困惑的事情是 GoogleStategy 是控制台记录用户配置文件和返回的访问令牌。我猜测当回调路由尝试验证来自 URL 的代码时会出现错误。谁能给我指出一个方向,以便更好地解决这个问题?提前致谢。