尝试使用Drive API v3 for Java和服务帐户向TeamDrive中的文件添加属性

时间:2017-09-13 15:26:38

标签: java google-drive-api

我尝试使用Drive API和服务帐户向已上传到TeamDrive的文件添加属性。我可以访问这些文件,但是当我尝试将属性映射添加到文件对象并更新()时,我收到403错误。

...

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();

...

并且它出现了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."
}

服务帐户具有相关TeamDrive的编辑权限。在v3文档中,它明确指出属性是可写的。所以我想知道我没有设置什么,或者我无意中创建了什么条件来禁止在驱动器文件上设置属性?

1 个答案:

答案 0 :(得分:1)

以下是与您的问题相关的SO post

从那里,您将被重定向到写为here的笔记。

如果您将阅读它,答案中有这一部分:

  

废纸篓/更新

     

我浪费了一些时间。过去很简单   service.files().trash(fileId).execute()。文件说

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

v2上update的{​​{3}}在文件上生成get,   设置新值,然后调用update

     

在v3上,使用类似update会抛出此异常:

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

解决方案是仅创建一个空的File设置新值:

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

注意:File是指com.google.api.services.drive.model.File(它   不是java.io.File)。

如需进一步阅读,请参阅以下相关文章