LoginSignup
1
0

More than 5 years have passed since last update.

Go言語からGoogleDriveAPI V3のUpdateを使おうとしたらfieldNotWritableエラーでつまづいて解決した

Posted at

結論

FilesService.Updateの第二引数で指定するdrive.File構造体のIdフィールドは空文字列 "" にする必要がある。

エラー内容と発生箇所のコード

googleapi: Error 403: The resource body includes fields which are not directly writable., fieldNotWritable
srv.Files.Update(fileId, file).Media(localFile).Do()

やりたかったこと

  • ローカルにあるファイルをGoogleドライブ上にコピーする。
    • Googleドライブ上の指定フォルダに同名のファイルが存在する場合は上書き、そうでなければ新規作成する。

つまづいたきっかけ

  • 上書き時に指定するfileIdを取得するため、Files.Listを使ってdrive.File構造体を取得した。
  • 取得した構造体を以下のように流用しようとした。

    srv.Files.Update(file.Id, file).Media(localFile).Do()
    

私が誤解したところ

  • FileのリファレンスにIdはwritableと書いてあり、エラー原因がここではないと勘違いした。
  • file.Id == fileId なら(≒実質的に変更が無ければ)エラーにはならないと勘違いした。

対象バージョンのAPI

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0