【问题标题】:Google_Service_Exception: fieldNotWritable when uploading a new version of the file on Google DriveGoogle_Service_Exception:在 Google Drive 上上传文件的新版本时 fieldNotWritable
【发布时间】:2017-05-08 13:24:17
【问题描述】:

我的目的是能够上传 Google Drive 上已有文件的新版本。

我从 https://developers.google.com/drive/v2/reference/files/update#examples 的 PHP 示例开始。

function updateFile($fileID, $uploadFile) {
    try {
        $file = $this->service->files->get($fileID);

        $content = file_get_contents($uploadFile);
        $file = $this->service->files->update($fileID, $file, array(
            'data' => $content,
        ));
        printf("Updated File ID: %s\n", $file->getId());
    } catch (Exception $e) {
        echo get_class($e), ': ', $e->getMessage(), PHP_EOL;
    }
}

结果我得到了

Google_Service_Exception: {
 "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."
 }
}

我不明白有问题的不可写字段是什么。我唯一要更改的是文件的实际内容,而不是元数据。

有什么想法吗?

【问题讨论】:

  • 这可能与 API v3 的变化有关吗?

标签: google-api google-drive-api google-apis-explorer google-php-sdk


【解决方案1】:

查看$file = $this->service->files->get($fileID) 返回的$file 对象。我的猜测是它包含一堆在https://developers.google.com/drive/v3/reference/files 处未定义为可写的字段

因此,当您在$file = $this->service->files->update($fileID, $file, 中向云端硬盘发送相同的 $file 对象时,云端硬盘会反对他们的存在。由于您只是在更新内容,因此您可以发送一个空的元数据对象来代替 $file。

此外,正如 Siawa 所指出的,您所关注的快速入门被标记为 v2,但如果您使用的是最新的 PHP 库,则可能已更改为 v3。这是否会影响快速入门是任何人的猜测。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 2020-03-10
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多