简体   繁体   English

我如何卷曲这篇文章?

[英]How do I curl this post?

I am trying to get an access Token from google api using code below, 我正在尝试使用以下代码从Google api获取访问令牌,

$grant = urlencode("urn:ietf:params:oauth:grant-type:jwt-bearer") . "&assertion=" . $jwtSign;
$postfields = array('Host' => 'www.googleapis.com', 'Content-Type' => 'application/x-www-form-urlencoded', 'POST' => '/oauth2/v3/token HTTP/1.1', 'grant_type' => $grant);

$headers = array(
    "Content-Type: application/x-www-form-urlencoded"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/oauth2/v3/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);

but I am getting error saying. 但我说错了。 Please help me what's wrong 请帮我怎么了

{
 "error": "invalid_request",
 "error_description": "Required parameter is missing: grant_type"
}

The value of the grant_type parameter now has become a concatenation of urn:..:jwt-bearer and the assertion. 现在, grant_type参数的值已成为urn:..:jwt-bearer和断言的串联。 They should be provided as separate parameters in the call to the token endpoint. 在调用令牌端点时,应将它们作为单独的参数提供。

You're also mixing setting header fields and providing POST parameters. 您还将混合设置标题字段并提供POST参数。

Lastly, providing an array to the CURLOPT_POSTFIELDS setting will make the Content-Type change to multipart/form-data . 最后,为CURLOPT_POSTFIELDS设置提供一个数组会将Content-Type更改为multipart/form-data

Rather use: 而是使用:

    $postfields = array(
      'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
      'assertion' => $jwtSign
    ); 

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/oauth2/v3/token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
    $result = curl_exec($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM