繁体   English   中英

如何修复 Google OAuth 401 错误无效客户端?

[英]How can I fix the Google OAuth 401 Error invalid client?

我正在使用 OAuth Google API 并且收到401 Error: invalid_client 这与clientIDclientSecret代码无关,因为我确信我完美地复制了密钥。 我再次创建了一个新凭据以确保它不是 Google API 的问题,但错误仍然存在。 当我运行服务器时,我也没有在控制台中收到任何错误。 有人可以帮助我吗?

这是我从浏览器收到的:

 > 401. That's an error. > > Error: invalid_client > > The OAuth client was not found. > > Request Details > response_type=code > redirect_uri=http://localhost:5000/auth/google/callback > scope=profile email > client_id=keys.googleClientID > That's all we know.

这是我的代码的一部分:

 passport.use(
      new GoogleStrategy(
        {
          clientID: 'keys.googleClientID',
          clienSecret: 'keys.googleClientSecret',
          callbackURL: '/auth/google/callback'
        },
        (accessToken, refreshToken, profile, done) => {
          console.log('access token', accessToken);
          console.log('refresh token', refreshToken);
          console.log('profile', profile);
        }
      )
    );

  app.get(
    '/auth/google',
    passport.authenticate('google',{
      scope: ['profile', 'email']
    })
  );
  app.get('/auth/google/callback', passport.authenticate('google'));

谢谢: :)

正如我在评论中提到的那样,在我看来,您正在发送“keys.googleClientID”,这不是您需要发送变量值的有效密钥。 keys.googleClientID

 passport.use(
      new GoogleStrategy(
        {
          clientID: keys.googleClientID,
          clientSecret: keys.googleClientSecret,
          callbackURL: '/auth/google/callback'
        },
        (accessToken, refreshToken, profile, done) => {
          console.log('access token', accessToken);
          console.log('refresh token', refreshToken);
          console.log('profile', profile);
        }
      )
    );

您应该确保启用了 Google+ API,并且此类似修复可以帮助您,其信息范围从获取新密钥到设置可以在 Google Developers Console 的同意屏幕部分为您的项目设置的产品名称。 在左侧导航和 select 同意屏幕中查看 APIs & auth。 您还需要在产品名称上方的框中设置您的 email 地址。 在 Google 开发者控制台的同意屏幕中设置 EMAIL ADDRESS 和 PRODUCT NAME,也解决了错误

如果您使用的是配置模块const config = require("config"); const keys = config.get('keys'); const config = require("config"); const keys = config.get('keys'); 你能用模板字符串试试这个吗

passport.use( new GoogleStrategy( { clientID: ${keys.googleClientID}`,在$之前和之后使用反引号}

      clientSecret: `${keys.googleClientSecret}`, //do like this for the above clientID 
      callbackURL: '/auth/google/callback'
    },
    (accessToken, refreshToken, profile, done) => {
      console.log('access token', accessToken);
      console.log('refresh token', refreshToken);
      console.log('profile', profile);
    }
  )
);

那是反引号 ${keys.googleClientID} 反引号我说反引号,因为stackoverflow会格式化反引号(``)

或使用实际变量passport.use( new GoogleStrategy( { clientID: keys.googleClientID, clientSecret: keys.googleClientSecret, callbackURL: '/auth/google/callback' }, (accessToken, refreshToken, profile, done) => { console.log('access token', accessToken); console.log('refresh token', refreshToken); console.log('profile', profile); } ) ); 另外,您在代码中还拼错了 clientSecret 它是 clienSecret

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM