Hi!
Upload the file to Google Drive with the Android app There are times when you want to share that file with others.
This time I will write a process to grant authority to the uploaded file!
You can set multiple permissions (setType)! !! -For each user. (Set Google Account name and email) -For each group. (Google Group email) -For each domain. (Com.jp, etc.) ・ Other (anyone).
CreatePermission.java
/**
*Permission settings
* @param driveService drive
* @param fileId fileID
* @throws IOException create permission happen IOException
*/
private void createPermission(Drive driveService,String fileId) throws IOException{
JsonBatchCallback<Permission> callback = new JsonBatchCallback<Permission>() {
@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
// Handle error
System.err.println(e.getMessage());
}
@Override
public void onSuccess(Permission permission, HttpHeaders responseHeaders) {
// Handle success
System.out.println("Permission ID: " + permission.getId());
}
};
BatchRequest batch = driveService.batch();
Permission userPermission = new Permission()
.setType("anyone")
.setRole("reader");
driveService.permissions().create(fileId, userPermission)
.setFields("id")
.queue(batch, callback);
batch.execute();
}
!! When removing permissions! It is necessary to obtain the ID required for deletion in advance.
DeletePermissions.java
File fileData; //File information of Google Drive you want to delete
for(Permission permission : fileData.getPermissions()){
driveService.permissions()
//File ID for which permission is to be deleted, permission ID.
.delete(fileData.getId() , permission.getId())
.queue(batch,callback);
}
!! Other! If you set permissions on the Google folder (boss), The files (minions) that belong to that boss have the same privileges. But be careful, it doesn't make sense to delete that permission for your minions! !! Defeat the boss or prepare another boss!
After that, please adjust the atmosphere. Please comment if anything happens. More details can be found at the URL below.
Recommended Posts