Unable to update Google Drive files using Drive API v3 — The resource body includes fields which are not directly writable

后端 未结 3 1030
不思量自难忘°
不思量自难忘° 2020-12-11 10:26

I am trying to use Google Drive API (v3) to make updates to documents in Google Drive.

I have read this migration guide: Google Drive API v3 Migration

An

相关标签:
3条回答
  • 2020-12-11 10:44

    in the code it looks like this

    Drive service... // your own declared implementation of service
    File  file = new File(); //using the com.google.api.services.drive.model package
        // part where you set your data to file like:
    file.setName("new name for file");
    String fileID = "id of file, which you want to change";
    service.files().update(fileID,file).execute();
    

    trying to change the fields from remote files, and rewriting to this file can throw the security exception like exception below. but it is not a solution for your question. If you want to share file to another google account by email, you can do it with reimplementing authorization to authorization with using service account of your app, and the add the needed email, as owner of the file.

    0 讨论(0)
  • 2020-12-11 10:45

    Referring to https://developers.google.com/drive/v3/reference/files#resource-representations, you can see that shared isn't a writable field. If you think about it, this makes perfect sense. You can share a file by adding a new permission, and you can check if a file has been shared by reading the shared property. But saying a file is shared, other than by actually sharing it, makes no sense.

    0 讨论(0)
  • 2020-12-11 10:46

    I had the same issue and found a solution. The key point is: you must create a new File object without Id and use it in update() method. Here is a piece of my code:

    val oldMetadata = service!!.files().get(fileId.id).execute()
    
    val newMetadata = File()
    newMetadata.name = oldMetadata.name
    newMetadata.parents = oldMetadata.parents
    newMetadata.description = idHashPair.toDriveString()
    
    val content = ByteArrayContent("application/octet-stream", fileContent)
    
    val result = service!!.files().update(fileId.id, newMetadata, content).execute()
    

    It works. I hope it'll help you.

    0 讨论(0)
提交回复
热议问题