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

问题描述:

我试图向已使用云端硬盘API和服务帐户上传到TeamDrive的文件添加属性。我可以访问这些文件,但是当我尝试将一个属性映射添加到一个文件对象并更新()时,我得到一个403错误。尝试使用适用于Java和服务帐户的Drive API v3为TeamDrive中的文件添加属性

... 

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文档中,它明确指出属性是可写的。所以我想知道我没有设置什么,或者我无意中创建了什么条件来禁止在驱动器文件上设置属性?

这是与您的问题相关的SO post

从那里,你将被重新定向到被写入here的笔记。

如果你读它,有这部分的答案:

垃圾桶/更新

我浪费了一些时间与这一个。曾经是一个简单的 service.files().trash(fileId).execute()。文件说

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

在V2的example codeupdate使得上档, 套新值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(); 

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

对于进一步阅读,这里有一些相关的帖子

+0

你是我的英雄。发现。 – user2458080