0

我的意圖是能夠上載Google雲端硬盤中已有的新版本文件。Google_Service_Exception:在Google雲端硬盤上上傳文件新版本時的fieldNotWritable

我已經從PHP示例https://developers.google.com/drive/v2/reference/files/update#examples開始了。

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." 
} 
} 

我不明白什麼是有問題的不是寫的領域。我正在改變的唯一事情就是文件的實際內容,而不是它的元數據。

任何想法有什麼不對?

+0

可能與API的v3中的更改有任何關係? – Slawa

回答

1

看看$file = $this->service->files->get($fileID)返回的$file對象。我的猜測是,它包含一堆未定義爲可寫入的字段https://developers.google.com/drive/v3/reference/files

因此,當您向$file = $this->service->files->update($fileID, $file,中的驅動器發送相同的$文件對象時,Drive會反對它們的存在。由於您只更新內容,因此您可以發送一個空的元數據對象來代替$ file。另外,正如Siawa指出的那樣,您所追隨的快速入門標籤爲v2,但如果您使用的是最新的PHP庫,則可能已更改爲v3。任何人的猜測都是否影響快速入門。

相關問題