Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get permissionId in Drive API v3?

I want to delete permission from the file.

In Drive API v2,

PermissionId permissionId = service.permissions().getIdForEmail(account).execute();
service.permissions().delete(fileId, permissionId.getId()).execute();

But According to document, permissions().getIdForEmail(String email) is removed and alternate is nothing.

https://developers.google.com/drive/v3/web/migration

I can't find how to get permissionId from specific Email address in drive API v3.

Do you have any idea?

like image 973
gaku Avatar asked Feb 25 '16 08:02

gaku


People also ask

How do I get permission for Google Drive?

Get permission to open a file Open the file. On the "You need permission" page, click Request access. The owner of the file will get an email asking for approval. After they approve your request, you'll get an email.

What are drive permissions?

A permission for a file. A permission grants a user, group, domain or the world access to a file or a folder hierarchy.


2 Answers

I found a simple solution:

        PermissionList permissions = service.permissions().list(sharedFolderId).setFields("nextPageToken, permissions(id,emailAddress)").execute();
        for (Permission p : permissions.getPermissions()) {
            if (p.getEmailAddress().equals(adresseEmail)) {
                service.permissions().delete(sharedFolderId, p.getId()).execute();
            }
        }
like image 127
vacolane Avatar answered Oct 12 '22 14:10

vacolane


Two years later, but your question was the first result I found when searching for a solution. I found a workaround and I hope this will help others with the same issue. This is what I did to get the permission id:

this.getPermissionId = function(emailAddress) {
    return new Promise((resolve, reject) => {

      const input = {
        q:  '"' + emailAddress + '" in writers or "' + emailAddress + '" in readers',
        fields: 'files(permissions)',
        pageSize: 1
      };

      const request = gapi.client.drive.files.list(input);

      request.execute(result => {
        if(result.error) {
          reject(result.error);
        } else if(result.files && result.files[0] && result.files[0].permissions && result.files[0].permissions[0]) {
          const permissions = result.files[0].permissions;
          let permissionId;
          permissions.forEach(permission => {
            if(permission.emailAddress == emailAddress) {
              permissionId = permission.id;
            }
          });

          if(permissionId) {
            resolve(permissionId);
          }

          else {
            reject('permissionIdUndefined');
          }
        }
      });


    })
  };
like image 22
Erik van den Hoorn Avatar answered Oct 12 '22 13:10

Erik van den Hoorn