Passport + Google Node + Express 应用程序中的令牌不断向 Postman 的尝试抛出 'Unauthorized',令牌是好的并且手动验证

Passport + Google Token in Node + Express app keeps throwing 'Unauthorized' to attempts from Postman, token is good and manually verified

我有一个使用 Node + Express + Passport 构建的后端 REST API,我正在尝试使用 google 访问令牌进行身份验证。我正在使用 this strategy.

我在 github 上查看了软件包的文档和问题,但没有解决问题。

我已经验证了访问令牌:

https://www.googleapis.com/oauth2/v1/tokeninfo?idToken with the id Token 和 https://www.googleapis.com/oauth2/v1/tokeninfo?acessToken with the access token 两者都是有效的,但其中 none 有效。我已经双重检查我在后端和前端使用了正确的 clientID 和密码,我从中获取了令牌。

相关代码如下:

    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 -- 当我从邮递员那里开火时,没有任何记录。当我从 angular 前端触发时——它记录数据并触发。

在我的例子中,这是因为我什至没有定义的 User.findOrCreate 逻辑而被抛出的。 Facebook 令牌护照包因此抛出一个内部服务器错误,但 google 一个会静静地失败,没有任何消息。

稍后我将在开发过程中定义我的 findOrCreate 逻辑。目前,这适用于测试:

  new GoogleTokenStrategy(
    {
      clientID: config.get('google.clientID'),
      clientSecret: config.get('google.clientSecret')
    },
    function(accessToken, refreshToken, profile, done) {
      return done(null, profile);
    }
  )
);