简体   繁体   English

Curl PHP Api V4 Google 表格

[英]Curl PHP Api V4 Google Sheets

with the following code I call the API v4 to print the values of a spreadsheet.使用以下代码,我调用 API v4 来打印电子表格的值。 It is going very well.进展非常顺利。

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"https://sheets.googleapis.com/v4/spreadsheets/
spreadsheetID/values/PHP?valueRenderOption=UNFORMATTED_VALUE&key=yourKeyId");
$result = curl_exec ($curl);
curl_close ($curl);

What I can't achieve is to do an insert or update on it, I can't manage to put the PHP curl together for this.我无法实现的是对其进行插入或更新,我无法为此将 PHP curl 放在一起。 Some help?一些帮助? Please try as below but it doesn't work.请尝试以下方法,但它不起作用。

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"https://sheets.googleapis.com/v4/spreadsheets/
spreadsheetID/append/PHP?InsertDataOption=INSERT_ROWS&key=yourKeyId");
$jvalues=array(array("5869","35685987","Ema","2020-05-29","no brinda","3563430423"));

$jsonData = array(
'values' => $jvalues
 );

$jsonDataEncoded = json_encode($jsonData);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec ($curl);
curl_close ($curl);

image for error错误图片

Some help, I found no examples anywhere.一些帮助,我在任何地方都找不到示例。 Thanks.谢谢。

Unfortunately, the API key cannot be used for the POST methods while it can be used for the GET method.不幸的是,API 密钥不能用于 POST 方法,但可以用于 GET 方法。 So it is required for putting values to Google Spreadsheet to use the access token retrieved by OAuth2 and/or the service account.因此,需要将值放入 Google 电子表格以使用由 OAuth2 和/或服务帐户检索到的访问令牌。 By the way, when the Google Spreadsheet is not yours or you have no permission for writing, even when the access token is used, the values cannot be used.顺便说一句,当 Google 电子表格不是您的或您没有写入权限时,即使使用访问令牌,也无法使用这些值。

References:参考:

Sheets Authorizing requests 表格授权请求

OAuth2 OAuth2

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

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