【问题标题】:Set “publish to web” in Google spreadsheet using Drive API (PHP)使用 Drive API (PHP) 在 Google 电子表格中设置“发布到网络”
【发布时间】:2017-08-14 16:22:08
【问题描述】:

我正在尝试使用 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."
 }
}

任何帮助将不胜感激!

【问题讨论】:

  • 参考这个SO post,可以试试去掉finalrevision参数吗?似乎示例仅使用了 fileId 和 revisionId。

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


【解决方案1】:

获取finalrevision时添加参数:

$finalrevision=$driveservice->revisions->get($sheet_id,$revision_id, array('fields' => 'publishAuto'));

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2012-09-27
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多