我尝试更新google驱动器文件权限,即尝试提供新角色。但它抛出异常。这是示例代码。
Permission content = new Permission();
permission.setRole("Reader");
permission.setType("user");
permission.setEmailAddress("x@gmail.com");
driveService.permissions().update(fileId, permissionId, permission).queue(batch, callback);
对于此示例代码,我得到了Json异常,如下所示
{
"error": {
"errors": [
{
"domain": "global",
"reason": "fieldNotWritable",
"message": "The resource body includes fields which are not directly writable."
}
],
"code": 403,
"message": "The resource body includes fields which are not directly writable."
}
}
有些人可以帮我解决这个问题吗?
答案 0 :(得分:1)
我犯了一个错误,即除了用户新角色之外还添加了用户电子邮件地址和类型。但是这两个字段在Drive REST API v3中不可覆盖。所以我得到了例外。更正的示例代码为foillows
Permission content = new Permission();
content.setId(id_of_the_file);
content.setRole("Reader");
driveService.permissions().update(fileId, permissionId, permission).setFields("role").execute();
答案 1 :(得分:0)
我认为您需要首先获得API的许可。你错过了这一行:
权限权限= service.permissions()。get( fileId,permissionId).execute();
像:
public class MyClass {
// ...
/**
* Update a permission's role.
*
* @param service Drive API service instance.
* @param fileId ID of the file to update permission for.
* @param permissionId ID of the permission to update.
* @param newRole The value "owner", "writer" or "reader".
* @return The updated permission if successful, {@code null} otherwise.
*/
private static Permission updatePermission(Drive service, String fileId,
String permissionId, String newRole) {
try {
// First retrieve the permission from the API.
Permission permission = service.permissions().get(fileId, permissionId).execute();
permission.setRole(newRole);
return service.permissions().update(fileId, permissionId, permission).execute();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
return null;
}
// ...
}
文档here中的更多详细信息。