【问题标题】:How can I fix the Google OAuth 401 Error invalid client?如何修复 Google OAuth 401 错误无效客户端?
【发布时间】:2020-02-07 22:39:56
【问题描述】:

我正在使用 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'));

谢谢! :)

【问题讨论】:

  • invalid_client 表示客户端 ID 和/或密码对您正在使用的代码无效。(未找到 OAuth 客户端 google 并没有对您撒谎)所以它肯定有一些东西与客户有关。如果您确定您正确复制了它们并且没有在开发控制台上删除它们。您创建了哪种类型的客户端?
  • 感谢您的回答,但是客户类型是什么意思?
  • 我不是节点期望但你不是发送字符串keys.googleClientID

标签: javascript node.js oauth google-oauth google-developers-console


【解决方案1】:

正如我在评论中提到的,在我看来,您正在发送“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);
        }
      )
    );

【讨论】:

    【解决方案2】:

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

    如果您使用的是配置模块 那是 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);
        }
      )
    );
    

    那是 backtick ${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

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • 上面的代码是否与您项目中的代码完全相同
    猜你喜欢
    • 2016-03-15
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2014-06-18
    • 2015-11-19
    相关资源
    最近更新 更多