google-api - OAuth2.0 token 奇怪的行为(无效凭证 401)

标签 google-api google-oauth

通常情况下,Google OAuth2.0 机制运行良好。

  1. 用户确认有权访问选定范围的 Google 帐户。
  2. 检索刷新 token 并将其保存到长期存储中。
  3. 每次需要时(如果访问 token 已过期)都会检索访问 token 并将其用于访问 API。

但有时(到目前为止,在 6 个多月的时间里只有两次)我经历过奇怪的行为:

对 Google API 的请求返回无效凭据 (401) 错误。 刷新访问 token (使用存储的刷新 token )没有帮助。

这是我在测试此问题时得到的一些结构化输出:

    + ------------------------------------------------------------------------- + 
    | 1.TRYING TO REFRESH THE TOKEN.                                            |
    | 2.DONE REFRESHING THE TOKEN.                                              |
    + ------------------------------------------------------------------------- + 
    |    access:           **************************************************** | 
    |   refresh:                  ********************************************* | 
    |   expires:                                                           3600 | 
    |   created:                                            2013-07-23 13:12:36 | 
    + ------------------------------------------------------------------------- + 

我还尝试通过将请求发送到来验证“新鲜”访问 token https://www.googleapis.com/oauth2/v1/tokeninfo

    + ------------------------------------------------------------------------- + 
    | 1. TRYING TO CHECK THE TOKEN .                                            |
    | 2. DONE CHECKING THE TOKEN THE TOKEN.                                     |
    + ------------------------------------------------------------------------- + 
    |       issued_to:                  ************.apps.googleusercontent.com |
    |        audience:                  ************.apps.googleusercontent.com |
    |         user_id:                                             ************ |
    |      expires_in:                                                     3600 |
    |           email:                                     **********@gmail.com |
    |  verified_email:                                                        1 |
    |     access_type:                                                  offline |
    |         scopes::                                                          |
    + ------------------------------------------------------------------------- + 
    | https://www.googleapis.com/auth/userinfo.email                            |
    | https://www.googleapis.com/auth/userinfo.profile                          |
    | https://www.googleapis.com/auth/plus.me                                   |
    | https://www.googleapis.com/auth/drive                                     |
    + ------------------------------------------------------------------------- + 

但是当我尝试访问驱动器源时,响应是:

    Error calling GET https://www.googleapis.com/drive/v2/files (401) Invalid Credentials

    domain:         global
    reason:         authError
    message:        Invalid Credentials
    locationType:   header
    location:       Authorization

我们在日历方面也遇到了同样的问题。 所以:

  1. token 之前有效(一切正常)。
  2. 刷新 token 仍然有效。
  3. 请求 Feed 时会返回“无效凭据”错误。
  4. 所有其他 token 仍然运行良好,这意味着代码有效。

通常,当 token 被撤销时,尝试刷新 token 时会返回“invalid_grant”错误。

问题

  1. 造成这种行为的原因是什么?如果刷新 token 被撤销或以其他方式变得无效,对新访问 token 的请求是否会产生错误?
  2. 有办法验证刷新 token 吗?

最佳答案

根据有关错误和错误代码的 Google API 文档:

https://developers.google.com/drive/handle-errors#401_invalid_credentials

401: Invalid Credentials

Invalid authorization header. The access token you're using is either expired or invalid.

error: {
  errors: [
   {
  "domain": "global",
  "reason": "authError",
  "message": "Invalid Credentials",
  "locationType": "header",
  "location": "Authorization",
  }
  ],
  "code": 401,
  "message": "Invalid Credentials"
  }
}

这与您的错误版本完全匹配,因此 Google 很可能认为您的请求有问题。

但是,正如您所知,Google API 请求可能会返回错误,这对于实际诊断问题显然没有帮助。由于多种原因,我收到了“无效凭据”错误。这几乎总是真的,因为我做了一些我认为无关紧要的改变,但实际上很重要。

我的第一个想法(这里是在黑暗中拍摄的)是转到 Google API 控制台:

https://code.google.com/apis/console

Google 身份验证 token 验证程序 ( https://www.googleapis.com/oauth2/v1/tokeninfo ) 可以返回有效响应,但客户端 key 或客户端 ID 可能已更改。

即使响应正文中的微小变化也可能导致此错误。

我不知道您如何发出请求,无论是通过 REST 调用还是客户端库,但我使用 ruby​​ 库,它允许命令行界面进行 API 调用。我发现这个和 OAuth2 Playground 对于诊断 Google API 调用非常有帮助。

仅供引用:我只从 Google API 收到 2 个错误:“凭据无效”和“权限不足”。后者几乎总是与不好的瞄准镜有关。前者几乎就是其他一切。

我还想说,如果您在 6 个月内只遇到 2 次错误,那么您很幸运!

关于google-api - OAuth2.0 token 奇怪的行为(无效凭证 401),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17813621/

相关文章:

machine-learning - Google Cloud Vision API(标签),有多少种可能的标签?

java - Google Drive API 给予上传文件的权限不足

google-oauth - 在没有域管理员启用 API 访问的情况下,如何访问域上的用户信息?

qt - Google OAuth 始终显示同意屏幕

android - 基于云的应用程序的 Google Play 服务?

android - 如何通过在 Android 中使用内容 uri(_not_ 文件 uri)的 Intent 传递单个图像

python - 如何在 Python 中使用 Google api 检索 picasa 照片

c# - Google Drive API V3,试图更改文档表和幻灯片以外的文件的所有权

node.js - 为什么谷歌Oauth verifyIdToken(javascript nodejs版本)不使用客户端 secret ?

python - Google Drive API 的刷新 token 何时到期?