[JAVA] Google Drive API Set File Permission (Set permissions on Google Drive files)

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.

https://developers.google.com/drive/api/v3/manage-sharing

Recommended Posts

Google Drive API Set File Permission (Set permissions on Google Drive files)
Google Drive file move
Upload JPG file using Google Drive API in Python
ls -R on Google Drive
Google Drive Api Tips (Python)
Python: Extract file information from shared drive with Google Drive API
Download Google Drive files in Python
Regularly upload files to Google Drive using the Google Drive API in Python
[Rails 6 / Google Map API] Post an address and set multiple markers on the map
Tweet (API 1.1) on Google App Engine for Python
Download files directly to Google Drive (using Google Colaboratory)
Upload files to Google Drive with Lambda (Python)