使用 oauth 2.0 google + 烦人的无效凭据应用程序编程接口

发布于 2024-12-10 04:34:10 字数 1135 浏览 1 评论 0原文

我正在尝试在我的网站上使用 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 请求的。这是我正在使用的过程:

  1. 让用户授权请求。
  2. 从 google 获取请求代码。
  3. 使用 cUrl 向 google 请求带有请求代码的访问令牌。
  4. 将访问代码放入 php 会话中。
  5. 重定向回主页。
  6. 主页检测到会话变量已设置并且不显示登录链接。
  7. 主页上的 PHP 使用 readFile 从 google 获取 json 响应。
  8. Google 返回无效凭据。

这是由 php 生成的示例 uri,插入到 readFile 中:

https://www.googleapis.com/plus/v1/people/me?prettyprint=true&access_token=ya29.AHES6ZQRGovDa5FHsojU3qCM1DEnYmJPywz1muUE4CWGH5n70OcAkw

请帮忙?

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:

  1. Get user to authorize the request.
  2. Gets request code from google.
  3. Uses cUrl to request access token with the request code from google.
  4. Puts the access code into a php session.
  5. redirects back to the main page.
  6. Main page detects session variable is set and doesn't display login link.
  7. Php on main page uses readFile to get the json response from google.
  8. Google returns invalid credentials.

here is a example uri generated by php that is inserted into readFile:

https://www.googleapis.com/plus/v1/people/me?prettyprint=true&access_token=ya29.AHES6ZQRGovDa5FHsojU3qCM1DEnYmJPywz1muUE4CWGH5n70OcAkw

Help please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

别想她 2024-12-17 04:34:10

您不应该共享未更改的访问令牌 - 有人可以使用它来冒充您(实际上是为授予它的人)。

最好将身份验证令牌作为标头传递,例如:

curl -H "Authorization: OAuth ya29.xyzxyz" "https://www.googleapis.com/plus/v1/people/me"

不确定这是否必要,但您的错误消息似乎表明标头中存在身份验证错误,因此您可能提供了与您需要的不匹配的授权标头。

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:

curl -H "Authorization: OAuth ya29.xyzxyz" "https://www.googleapis.com/plus/v1/people/me"

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.

够运 2024-12-17 04:34:10

这是使用 PHP 的 pecl oauth 扩展的解决方案。他们将按照您定义的方式签署请求。在本例中,在导入到脚本中的配置文件 json 对象中。

        $oauth = new OAuth($this->config->consumer_key, $this->config->consumer_secret, $this->config->signature_method, $this->config->auth_type);
        $oauth->setVersion($this->config->version);
        $oauth->setToken($accessToken->oauth_token, $accessToken->oauth_token_secret);

        $params = array(
            'fields' => 'displayName,emails,id,image,name',
            'pp' => 1
        );

        $oauth->fetch('https://www.googleapis.com/plus/v1/people/me', $params, OAUTH_HTTP_METHOD_GET);

        // extract response
        $json = Zend_Json::decode($oauth->getLastResponse(), Zend_Json::TYPE_OBJECT);

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.

        $oauth = new OAuth($this->config->consumer_key, $this->config->consumer_secret, $this->config->signature_method, $this->config->auth_type);
        $oauth->setVersion($this->config->version);
        $oauth->setToken($accessToken->oauth_token, $accessToken->oauth_token_secret);

        $params = array(
            'fields' => 'displayName,emails,id,image,name',
            'pp' => 1
        );

        $oauth->fetch('https://www.googleapis.com/plus/v1/people/me', $params, OAUTH_HTTP_METHOD_GET);

        // extract response
        $json = Zend_Json::decode($oauth->getLastResponse(), Zend_Json::TYPE_OBJECT);
那些过往 2024-12-17 04:34:10

您是否尝试过 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

中性美 2024-12-17 04:34:10

我以前也遇到过这个问题,不过是用 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.

江湖彼岸 2024-12-17 04:34:10

几个小时以来我一直收到相同的 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!

甜柠檬 2024-12-17 04:34:10

我认为 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}

留一抹残留的笑 2024-12-17 04:34:10

删除您的 token.json 文件,然后再次尝试请求。

Delete your token.json file, then attempt the request again.

盗琴音 2024-12-17 04:34:10

对我来说,问题是 GET/POST 请求上的标题“Authorization”:

Google 文档说: Authorization: /* OAuth 2.0 token here */

但正确的是: Authorization: OAuth /* OAuth 2.0 token here */

是的!在您的令牌密钥之前包含“OATH”!

如果您使用 cURL (PHP),请使用:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth '.$_SESSION['access_token'], 'Content-Type: application/json'));

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:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth '.$_SESSION['access_token'], 'Content-Type: application/json'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文