4

我正在使用 curl 为谷歌帐户使用以下发布方法,但它给了我 invalid_request 错误。

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&
client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&
client_secret=CENSORED&
redirect_uri=http://localhost/oauth2callback&
grant_type=authorization_code

这是我的带有 curl 的 PHP 代码

$text ='test';

$URL = "https://accounts.google.com/o/oauth2/token";

$header = array(
"POST /o/oauth2/token HTTP/1.1",
"Host: accounts.google.com",
"Content-type: application/atom+xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache", 
"code=[my_code]&client_id=[my_client_id]&client_secret=[my_client_secret]& redirect_uri=http://localhost/curl_resp.php&grant_type=authorization_code",
"Content-length: ".strlen($text),
);


 $xml_do = curl_init();
 curl_setopt($xml_do, CURLOPT_URL, $URL);
 curl_setopt($xml_do, CURLOPT_CONNECTTIMEOUT, 10);
 curl_setopt($xml_do, CURLOPT_TIMEOUT, 10);
 curl_setopt($xml_do, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($xml_do, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($xml_do, CURLOPT_SSL_VERIFYHOST, false);
 curl_setopt($xml_do, CURLOPT_POST, false);
 curl_setopt($xml_do, CURLOPT_POSTFIELDS, $text);
 curl_setopt($xml_do, CURLOPT_HTTPHEADER, $header);

我有无效的请求错误

4

5 回答 5

8

我对使用 Google 的 oAuth API 一无所知,但从我目前看到的示例来看,您应该在 post 字段中传递值(即code,client_id等),而不是直接在HTTP 标头。

下面的例子仍然不能完全工作,但它没有得到invalid_request错误,而是给你invalid_grant. 我认为除了我提到的内容之外还有其他问题(也许您需要来自 Google 的新凭据或其他东西),但这可能会让您更近一步,至少:

$post = array(
    "grant_type" => "authorization_code", 
    "code" => "your_code", 
    "client_id" => "your_client_id", 
    "client_secret" => "your_client_secret", 
    "redirect_uri" => "http://localhost/curl_resp.php"
);

$postText = http_build_query($post);

$url = "https://accounts.google.com/o/oauth2/token";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postText); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$result = curl_exec($ch);
var_dump($result);    // gets an error, "invalid_grant"
于 2012-06-07T15:26:34.130 回答
1

对您的请求的响应实际上包括对问题的非常易读的描述:

POST 请求需要一个Content-length标头。

于 2012-06-07T13:14:35.213 回答
0

为什么将 CURLOPT_POST 设置为false

 curl_setopt($xml_do, CURLOPT_POST, false);

尽管如果设置了 CURLOPT_POSTFIELDS,cURL 的某些配置会自动将其更改为 tru,但您没有有效的 postfield。

在 HTTP“POST”操作中发布的完整数据。要发布文件,请在文件名前加上 @ 并使用完整路径。文件类型可以通过使用格式为“;type=mimetype”的文件名来明确指定。此参数可以作为 urlencoded 字符串(如 'para1=val1¶2=val2&...')或作为字段名称作为键和字段数据作为值的数组传递。如果 value 是一个数组,则 Content-Type 标头将设置为 multipart/form-data。从 PHP 5.2.0 开始,如果文件以 @ 前缀传递给此选项,则 value 必须是数组。

它应该是 param=value 语法或传递的数组。

于 2012-06-07T14:50:52.513 回答
0

我也遇到了同样的问题,谷歌返回一个“invalid_xxx”。

经过大量研究和故障排除,问题出在表单本身的编码上!不要使用函数“http_builder_query”,因为它会弄乱字符串并且谷歌无法“识别”它。例子 :

http_builder_query 输出:“代码=4%252FYYUT71KJ6...”

相比

只是一个普通的字符串:“code=4/YYUT71KJ6...”

这是我的工作代码,我在需要您自己的数据的位置放置了“x”(从google oauth authentication获取和修改)

    $code_from_user_login = "4/YYUT71KJ6....";
    $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


        $post_params = "code=" . $code_from_user_login . "&";
        $post_params .= "redirect_uri=http://www.x.com/&";
        $post_params .= "client_id=x.apps.googleusercontent.com&";
        $post_params .= "client_secret=x&";
        $post_params .= "grant_type=authorization_code&";


        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
        $result = curl_exec($ch);
        print($result);

结果将是:

  '{
  "access_token" : "ya29.AHES6ZSwJSHpTZ1t....",
  "token_type" : "Bearer",
  "expires_in" : 3599,
  "id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6IjU0MDQxOTZlMmQzOGNjYTA2MW...."
}'

获得“access_token”后,使用以下网址访问配置文件数据: https ://www.googleapis.com/oauth2/v1/userinfo?access_token=YOUR_ACCESS_TOKEN_HERE

~结束~

于 2013-08-22T17:17:40.220 回答
0

我在 CURL 方法和有效的 Google 代码中使用了 post 数组,它对我有用

于 2012-06-08T07:21:13.790 回答