我有一个使用 Node + Express + Passport 构建的后端 REST API,并且我正在尝试使用 google 访问 token 进行身份验证。我正在使用this strategy.
我已经查看了 github 上的软件包文档和问题,但没有任何内容可以解决该问题。
我已验证访问 token :
https://www.googleapis.com/oauth2/v1/tokeninfo?idToken
以及 id token 和 https://www.googleapis.com/oauth2/v1/tokeninfo ?accessToken
与访问 token ,两者都有效,但都不起作用。我进行了两次三重检查,确保我在后端使用了正确的 clientID 和 key ,并在前端获取了 token 。
相关代码如下:
app.use(passport.initialize());
passport.use(
new GoogleTokenStrategy(
{
clientID: config.get('google.clientID'),
clientSecret: config.get('google.clientSecret')
},
function(accessToken, refreshToken, profile, done) {
console.log(accessToken, refreshToken, profile, done)
User.findOrCreate({ googleId: profile.id }, function(err, user) {
return done(err, user);
});
}
)
);
app.use('/user', passport.authenticate('google-token'), userRoute);
我在顶部导入如下:
const passport = require('passport');
const GoogleTokenStrategy = require('passport-google-token').Strategy;
应用程序不会抛出任何类型的错误。
我在创建策略的地方添加了console.log——当我从 postman 那里触发时,没有任何记录。当我从有角度的前端发射时——它会记录数据并且正在发射。
最佳答案
在我的例子中,这是由于我什至没有定义的 User.findOrCreate 逻辑而被抛出的。 Facebook token Passport 包因此引发内部服务器错误,但 google 则只会默默地失败且没有任何消息。
稍后我将在开发过程中定义我的 findOrCreate 逻辑。目前,这适用于测试:
new GoogleTokenStrategy(
{
clientID: config.get('google.clientID'),
clientSecret: config.get('google.clientSecret')
},
function(accessToken, refreshToken, profile, done) {
return done(null, profile);
}
)
);
关于node.js - Node + Express 应用程序中的 Passport + Google token 不断向 Postman 的尝试抛出 'Unauthorized', token 良好且手动验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55167207/