1

I'm trying to add properties to files that have already been uploaded to a TeamDrive using the Drive API and a service account. I can access the files, but when I try to add a properties map to a file object and update() I get a 403 error.

...

Drive.Files.List request = service.files()
    .list()
    .setIncludeTeamDriveItems(true)
    .setCorpora("teamDrive")
    .setSupportsTeamDrives(true)
    .setTeamDriveId("MAZEPX3fk38ieDu9PVL")
    .setQ("name contains 'aad081cc-0929-42bf-88f9-cb43c5ed0742'");

FileList files = request.execute();
File f = files.getFiles().get(0);
Map<String, String> props = new HashMap<>();
props.put("fancyTag", "this_is_my_tag_value");
f.setProperties(props);
service.files().update(f.getId(),f).execute();

...

and it blows up with a com.google.api.client.googleapis.json.GoogleJsonResponseException:

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

The service account has edit access to the TeamDrive in question. In the v3 docs, it says explicitly that properties are writable. So I'm wondering what have I not set, or what conditions have I inadvertently created to disallow setting properties on drive files?

user2458080
  • 989
  • 8
  • 17

1 Answers1

2

Here is a SO post related to your problem.

From there, you will be re-directed to the notes that was written here.

If you will read it, there is this part in the answer:

Trash / Update

I wasted some time with this one . Used to be a simple service.files().trash(fileId).execute(). Docs say

files.trash -> files.update with {'trashed':true}

The example code for update on v2 makes a get on the file, sets new values and then calls update.

On v3, using update like that throws this exception:

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

The solution is to create an empty File setting only the new values:

File newContent = new File();
newContent.setTrashed(true);
service.files().update(fileId, newContent).execute();

Note: File refers to com.google.api.services.drive.model.File (it is not java.io.File).

For further reading, here are some related post

MαπμQμαπkγVπ.0
  • 5,887
  • 1
  • 27
  • 65