使用 oauth 2.0 google + 烦人的无效凭据应用程序编程接口
我正在尝试在我的网站上使用 oauth 2.0 作为 google + api,但我不断收到:
{
"error": {
"errors": [{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}],
"code": 401,
"message": "Invalid Credentials"
}
}
问题是,我不知道为什么会发生这种情况。我有来自谷歌的有效访问令牌,但谷歌告诉我它是无效的。我知道令牌尚未过期,因为 json 数据是在获取访问令牌后 10 秒内从 google 请求的。这是我正在使用的过程:
- 让用户授权请求。
- 从 google 获取请求代码。
- 使用
cUrl
向 google 请求带有请求代码的访问令牌。 - 将访问代码放入 php 会话中。
- 重定向回主页。
- 主页检测到会话变量已设置并且不显示登录链接。
- 主页上的 PHP 使用
readFile
从 google 获取 json 响应。 - Google 返回无效凭据。
这是由 php 生成的示例 uri,插入到 readFile 中:
请帮忙?
I'm trying to use oauth 2.0 for the google + api on my site, and I keep getting:
{
"error": {
"errors": [{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}],
"code": 401,
"message": "Invalid Credentials"
}
}
The thing is, I don't know why this is happening. I have a valid access token from google, but google tells be it is invalid. I know that the token has not expired because the json data is request from google within 10 seconds of getting the access token. Here is the process that I'm using:
- Get user to authorize the request.
- Gets request code from google.
- Uses
cUrl
to request access token with the request code from google. - Puts the access code into a php session.
- redirects back to the main page.
- Main page detects session variable is set and doesn't display login link.
- Php on main page uses
readFile
to get the json response from google. - Google returns invalid credentials.
here is a example uri generated by php that is inserted into readFile:
Help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您不应该共享未更改的访问令牌 - 有人可以使用它来冒充您(实际上是为授予它的人)。
最好将身份验证令牌作为标头传递,例如:
不确定这是否必要,但您的错误消息似乎表明标头中存在身份验证错误,因此您可能提供了与您需要的不匹配的授权标头。
You shouldn't share an unaltered access token - someone can use that to impersonate you (really for whomever it was granted).
It's also better to pass the Auth token as a header, like:
Not sure if that's essential but your error message seems to indicate an auth error in the header so you may be providing an Authorization header which doesn't match the one you need.
这是使用 PHP 的 pecl oauth 扩展的解决方案。他们将按照您定义的方式签署请求。在本例中,在导入到脚本中的配置文件 json 对象中。
Here is a solution using PHP's pecl oauth extension. The will sign the request the way you have defined it. In this case in a config file json object that was imported into the script.
您是否尝试过 Google API 客户端之一?您可以使用一些入门应用程序来开始工作。
https://developers.google.com/+/downloads
Have you tried one of the Google API clients? There are starter applications you can use to get the ball rolling.
https://developers.google.com/+/downloads
我以前也遇到过这个问题,不过是用 Twitter 的。
对于 OAuth 实际上,我们与 twitter 通信两次,第一次获取请求令牌,第二次授权发送已签名的第一个令牌。
也许你只克服了第一个。
I had this problem before but with twitter.
For OAuth actually we communicate with the twitter twice, first to acquire request token, second to authorize sending the first token that's already signed.
Maybe you only overcome the 1st one.
几个小时以来我一直收到相同的 401“无效凭据”错误。我注意到我将 access_token 存储在数据库中的 VARCHAR(50) 字段中。它切断了access_token的一部分。我增加了列的长度。固定的。
仔细检查存储 access_token 和刷新令牌的数据库中字段的长度!
I have been getting the same 401 "Invalid Credentials" error for a few hours. Than I noticed that I stored my access_token in the database in a VARCHAR(50) field. It cut off a portion of the access_token. I increased the column length. FIXED.
Double check the length of the field in the database where you store your access_token and also your refresh_token!
我认为
me
API 已损坏。当我尝试请求具有真实用户 ID 的 URI 时,问题就消失了。我的意思是这样的:https://www.googleapis.com/plus/v1/people/108189587050871927619?key={your_api_key}
I think the
me
API is broken. The problem is gone when I try to request a URI with a real user ID. I mean like this:https://www.googleapis.com/plus/v1/people/108189587050871927619?key={your_api_key}
删除您的 token.json 文件,然后再次尝试请求。
Delete your token.json file, then attempt the request again.
对我来说,问题是 GET/POST 请求上的标题“Authorization”:
Google 文档说: Authorization: /* OAuth 2.0 token here */
但正确的是: Authorization: OAuth /* OAuth 2.0 token here */
是的!在您的令牌密钥之前包含“OATH”!
如果您使用 cURL (PHP),请使用:
The problem for me was the header "Authorization" on GET/POST request:
Google documentation said: Authorization: /* OAuth 2.0 token here */
But the correct is: Authorization: OAuth /* OAuth 2.0 token here */
Yes! include "OATH " before your token key!
If you are using cURL (PHP), use: