Reputation: 101
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?
Upvotes: 6
Views: 3519
Reputation: 155
I use UrlFetchApp with Google Apps Script to replace Drive API v2 and Advanced Drive Service (based on v2).
With a company domain service account, the section getService(userEmail)
uses the library https://github.com/googleworkspace/apps-script-oauth2 to send the request on behalf of userEmail
.
/**
* Get user permission Id.
*
* @param {String} userEmail - Email address for About query.
* https://developers.google.com/drive/api/v3/reference/about
*/
function TEST_getIdForEmailV3() { getIdForEmailV3('[email protected]') }
function getIdForEmailV3(userEmail) {
var service = getService(userEmail);
if (service.hasAccess()) {
var url = 'https://www.googleapis.com/drive/v3/about' + '?fields=user/permissionId';
var options = {
'method': 'get',
'contentType': 'application/json',
'headers': { Authorization: 'Bearer ' + service.getAccessToken() }
};
var response = UrlFetchApp.fetch(url, options);
var resultParsed = JSON.parse(response.getContentText());
return resultParsed.user.permissionId;
} else {
return 0;
};
}
Upvotes: 0
Reputation: 21
I have done this code in .NET using C#.
I hope you have already created the drive service using user's access token.
After that this code can help you to get permission ID:
var permissionFile = driveService.About.Get();
permissionFile.Fields = "*";
var perm = permissionFile.Execute();
permissionId = perm.User.PermissionId;
The permissionId
will give you the required ID.
Upvotes: 0
Reputation: 167
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();
}
}
Upvotes: 3
Reputation: 96
a .NET version that solved my needs
public static string GetPermissionIdForEmail(DriveService service, string emailAddress)
{
string pageToken = null;
do
{
var request = service.Files.List();
request.Q = $"'{emailAddress}' in writers or '{emailAddress}' in readers or '{emailAddress}' in owners";
request.Spaces = "drive";
request.Fields = "nextPageToken, files(id, name, permissions)";
request.PageToken = pageToken;
var result = request.Execute();
foreach (var file in result.Files.Where(f => f.Permissions != null))
{
var permission = file.Permissions.SingleOrDefault(p => string.Equals(p.EmailAddress, emailAddress, StringComparison.InvariantCultureIgnoreCase));
if (permission != null)
return permission.Id;
}
pageToken = result.NextPageToken;
} while (pageToken != null);
return null;
}
Upvotes: 1
Reputation: 603
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');
}
}
});
})
};
Upvotes: 1