使用Drive API(PHP)在Google电子表格中设置“发布到网络”

时间:2017-08-14 16:22:09

标签: php google-drive-api google-sheets-api

我正在尝试使用Google Drive API将Google表格设置为"发布到网络"模式,以便我可以将其嵌入我的网站。

在我的PHP中使用Drive API: -

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Sheets($client);

$driveservice = new Google_Service_Drive($client);
/*
$copiedFile = new Google_Service_Drive_DriveFile(array('name' => 'Project plan'));
$responseBody=$driveservice->files->copy("1hAjKEox4hhlK4aoCVLuUVfTJbKPhFklG1gG9-HsGH7U", $copiedFile);
$sheet_id=$responseBody->id;
*/
$sheet_id="1tYktONmPaaxGaS5ylNn8nTzPjtdEsuYIDHakhap8Amk";
$revisions=$driveservice->revisions;
$sheetinformation =$revisions->listRevisions("1tYktONmPaaxGaS5ylNn8nTzPjtdEsuYIDHakhap8Amk");
$revisions=$sheetinformation->getRevisions();
$count=count($revisions);
$revision_id=$revisions[$count-1]->id;
$finalrevision=$driveservice->revisions->get($sheet_id,$revision_id);
$finalrevision->publishAuto=true;
$driveservice->revisions->update($sheet_id,$revision_id, $finalrevision);

die();

据我所知,这符合Drive API规范(我真的希望他们没有删除PHP的代码示例!)。但是,我在响应正文中返回以下错误: -

"error": {
  "errors": [
   {
    "domain": "global",
    "reason": "fieldNotWritable",
    "message": "The resource body includes fields which are not directly writable."
   }
  ],
  "code": 403,
  "message": "The resource body includes fields which are not directly writable."
 }
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

在获得最终结果时添加参数:

[186.797, 187.272, 187.747, 188.222, 188.697, 189.172, 189.646, 190.121]
[-48, -48, -48, -45, -43, 6, -23, 12]

如果没有last参数,get方法将返回整个修订版(包括所有字段,包括只读参数)。