使用HTTP请求使用spreadsheets.values.update方法Google API

时间:2018-01-08 21:19:09

标签: php api http google-api

我希望使用以下API来更新一个单元格: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update

我无法让API库与我的软件包一起使用,所以我一直在使用HTTP请求来处理其他情况。

对于这种情况,这是一种类似的方法:

$request_url = "https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID/values/Sheet2!A2";
$access_token = get_access_token();
$headers = array();
$headers[] = 'Authorization: Bearer ' . $access_token;
try {
  $data = array('valueInputOption' => 'RAW',
                'values' => [0 => strval($post_id)]);
  $curl = new \MyApp\Http\Curl($request_url, array(
      CURLOPT_HTTPHEADER => $headers,
      CURLOPT_CUSTOMREQUEST => "PUT",
      CURLOPT_POSTFIELDS => http_build_query($data)
  ));
  $response_json = $curl->__toString();
  var_dump($response_json);
} catch (\RuntimeException $ex) {
  die(sprintf('Http error %s with code %d', $ex->getMessage(), $ex->getCode()));
}

但我明白了:

string(573) "{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"values[0]\": Cannot bind query parameter. Field 'values[0]' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"values[0]\": Cannot bind query parameter. Field 'values[0]' could not be found in request message."
          }
        ]
      }
    ]
  }
}
"

如何在此API请求中发布值?

以下是Google网页的试用此API部分。

enter image description here

0 个答案:

没有答案