2

我有一个使用 Node + Express + Passport 构建的后端 REST API,我正在尝试使用谷歌访问令牌进行身份验证。我正在使用这种策略。

我在 github 上查看了包的文档和问题,但没有任何东西可以解决这个问题。

我已经验证了访问令牌:

https://www.googleapis.com/oauth2/v1/tokeninfo?idToken使用 id 令牌和https://www.googleapis.com/oauth2/v1/tokeninfo?acessToken访问令牌,两者都是有效的,但它们都不起作用。我已经双重检查了我在后端使用了正确的 clientID 和 secret,在前端我从中获取了令牌。

以下是相关代码:

    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——当我从邮递员那里开火时,没有记录任何内容。当我从有角度的前端发射时——它会记录数据并发射。

4

1 回答 1

0

就我而言,这是因为我什至没有定义的 User.findOrCreate 逻辑而被抛出的。Facebook 令牌护照包因此会引发内部服务器错误,但谷歌会默默地失败,没有任何消息。

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

  new GoogleTokenStrategy(
    {
      clientID: config.get('google.clientID'),
      clientSecret: config.get('google.clientSecret')
    },
    function(accessToken, refreshToken, profile, done) {
      return done(null, profile);
    }
  )
);
于 2019-03-15T10:42:19.797 回答