Dar*_*man 37 google-api google-oauth
通常,Google OAuth2.0机制运行良好.
但有时候(迄今为止只有两次超过6个月)我经历过一些奇怪的行为:
对Google API的请求返回Invalid Credentials(401)错误.刷新访问令牌(使用存储的刷新令牌)没有帮助.
以下是我在测试此问题时得到的一些结构化输出:
+ ------------------------------------------------------------------------- +
| 1.TRYING TO REFRESH THE TOKEN. |
| 2.DONE REFRESHING THE TOKEN. |
+ ------------------------------------------------------------------------- +
| access: **************************************************** |
| refresh: ********************************************* |
| expires: 3600 |
| created: 2013-07-23 13:12:36 |
+ ------------------------------------------------------------------------- +
我还尝试通过向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 |
+ ------------------------------------------------------------------------- +
但是当我尝试访问驱动器Feed时,响应是:
Error calling GET https://www.googleapis.com/drive/v2/files (401) Invalid Credentials
domain: global
reason: authError
message: Invalid Credentials
locationType: header
location: Authorization
我们也遇到了与日历相同的问题.所以:
通常,当令牌被撤销时,尝试刷新令牌时会返回"invalid_grant"错误.
问题
Pau*_*cus 20
根据错误和错误代码的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"
}
}
Run Code Online (Sandbox Code Playgroud)
这与您的错误版本完全匹配,因此很可能是Google认为您的请求有误.
但是,众所周知,Google API请求可以返回对实际诊断问题明显无益的错误.出于多种原因,我收到了"Invalid Credentials"错误.这几乎总是因为我做了一些我认为无关紧要的改变,但确实如此.
我的第一个想法(在黑暗中拍摄)将转到Google API控制台:
https://code.google.com/apis/console
Googles身份验证令牌验证程序(https://www.googleapis.com/oauth2/v1/tokeninfo)可以返回有效的响应,但客户端密码或客户端ID可能已更改.
即使响应体中的微小变化也会导致此错误.
我不知道你是如何通过REST调用或客户端lib发出请求的,但我使用ruby lib,它允许命令行界面进行API调用.我发现这个和OAuth2 Playground非常有助于诊断Google API调用.
仅供参考:我只收到了来自Google API的2个错误:"凭据无效"和"权限不足".后者几乎总是与糟糕的范围有关.前者几乎就是其他一切.
我还要说,如果你在6个月内只遇到2次错误,那么你很幸运!
当我尝试在谷歌控制台中更改重定向网址然后在服务器上更新我的json凭据文件时,我遇到了这个问题.我必须在重新开始之前清除会话变量.所以在你的项目中只需执行一次:
session_start(); //starts a session
session_unset(); //flushes out all the contents previously set
Run Code Online (Sandbox Code Playgroud)
记得session_unset()在干运行后取下一次.
| 归档时间: |
|
| 查看次数: |
66900 次 |
| 最近记录: |