2016-04-27 168 views
0

我試圖更新谷歌驅動器文件的權限,即試圖給新的角色。但它拋出了一個例外。這裏是示例代碼。如何使用REST API更新Google Drive文件權限?

Permission content = new Permission(); 
permission.setRole("Reader"); 
permission.setType("user"); 
permission.setEmailAddress("[email protected]"); 
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

Java Drive REST API V3中可能如何實現這一點? – Venkat

回答

0

我認爲你需要首先獲得API的許可。您錯過了這一行:

權限權限= service.permissions()。get( fileId,permissionId).execute();

喜歡:在文檔here

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; 

} 

// ... 

} 

更多細節。

+0

這在驅動器REST API V2中工作正常,但不在V3中。有沒有其他更新V3的權限? – Venkat

1

我犯了一個錯誤,即添加用戶的電子郵件地址和類型,除了用戶的新角色之外,還有請求。但是這兩個字段在Drive REST API v3中不可覆蓋。所以我得到了例外。更正的示例代碼如同foowows

Permission content = new Permission(); 
content.setId(id_of_the_file); 
content.setRole("Reader"); 
driveService.permissions().update(fileId, permissionId, permission).setFields("role").execute(); 
相關問題