Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我该如何卷曲这根柱子?_Php_Curl_Google Api_Access Token - Fatal编程技术网

Php 我该如何卷曲这根柱子?

Php 我该如何卷曲这根柱子?,php,curl,google-api,access-token,Php,Curl,Google Api,Access Token,我正在尝试使用下面的代码从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' =&

我正在尝试使用下面的代码从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);
但我说的是错误的。请帮帮我怎么了
grant_type
参数的值现在已成为
urn:..:jwt bearer
和断言的串联。它们应该在对令牌端点的调用中作为单独的参数提供

您还混合设置标题字段和提供POST参数

最后,为
CURLOPT_POSTFIELDS
设置提供一个数组将使
内容类型
更改为
多部分/表单数据

而是使用:

    $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);

你需要自己编写一些代码。不幸的是,我们只提供代码帮助,而不是为您编写:)谢谢Hans,但您的意思是这样的$postfields=array('Host'=>'www.googleapis.com','Content Type'=>'application/x-www-form-urlencoded','POST'=>'/oauth2/v3/token HTTP/1.1','grant_Type'=>urlencode(“urn:ietf:params:oauth:grant-Type:jwt bearer”),'assertion'=>$jwtSign);它现在正在工作,但出现了一个错误:“错误描述”:“无效授权类型”“无效授权类型”或“无效授权”?{“错误”:“无效授权请求”,“错误描述”:无效的授权类型:urn:ietf:params:oauth:grant type:jwt bearrier&;assertion=eyj0exiaioijkv1qiljhbgcioijsuzi1nij9.eyjhdwqijoidhrwczlovczld3dy5nb29nbgv0adivdjmglmnvbs9vyxv0adivdjjzdgggg9rzw4ilcjzy29wzzzi6imh0bzoi8vc3byzwkwkkkk2wkzwkzwk2vzwzzwzzzzzwzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzNlNTFhMTVxOW5iQGRldmVsb3Blci5nc2VydmljZWFjY291bnQuY29tIn0。“}这意味着curl请求现在可以了;您发送的JWT断言的内容没有问题,但这是另一个问题/主题;fwiw:您应该遵循
    $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);