Unable to update Google Drive files using Drive AP

2019-02-27 20:04发布

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

And coded it to make a new empty File() with the details I want to update and then calling execute() with that and the file ID. But i am still getting an error. Can anyone point out where I am doing wrong? thanks alot!!

Error:

{
    "code" : 403,
    "errors" : [{
        "domain" : "global",

        "message" : "The resource body includes fields which are not directly writable.",

        "reason" : "fieldNotWritable"
    }],
    "message" : "The resource body includes fields which are not directly writable."
}

Code snippet below:

File newFileDetails = new File();        
FileList result = service2.files().list()
    .setPageSize(10)    
    .setFields("nextPageToken, files(id, name)")
    .execute();

List<File> files = result.getFiles();

if (files == null || files.size() == 0) {

    System.out.println("No files found.");

} else {

   System.out.println("Files:");

   for (File file : files) {

      if (file.getName().equals("first_sheet")) {

         System.out.printf("%s (%s)\n", file.getName(), file.getId());


         newFileDetails.setShared(true);


         service2.files().update(file.getId(), newFileDetails).execute();

      }

   }

}

2条回答
forever°为你锁心
2楼-- · 2019-02-27 20:27

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.

查看更多
一夜七次
3楼-- · 2019-02-27 20:49

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.

查看更多
登录 后发表回答