我想从文件中删除权限。
在Drive API v2中,
PermissionId permissionId = service.permissions().getIdForEmail(account).execute();
service.permissions().delete(fileId, permissionId.getId()).execute();
但是根据文档,权限()。getIdForEmail(字符串电子邮件)被删除,而备用则没有。
https://developers.google.com/drive/v3/web/migration
我无法找到如何从驱动API v3中的特定电子邮件地址获取permissionId。
你有什么想法吗?
答案 0 :(得分:1)
两年后,但您的问题是我在搜索解决方案时找到的第一个结果。我找到了一个解决方法,我希望这会帮助其他人解决同样的问题。这就是我为获取权限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');
}
}
});
})
};
答案 1 :(得分:1)
我找到了一个简单的解决方案:
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();
}
}
答案 2 :(得分:0)
解决了我的需求的.NET版本
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;
}
答案 3 :(得分:0)
我已经使用C#在.NET中完成了这段代码。
我希望您已经使用用户的访问令牌创建了驱动器服务。
之后,此代码可以帮助您获取许可ID:
var permissionFile = driveService.About.Get();
permissionFile.Fields = "*";
var perm = permissionFile.Execute();
permissionId = perm.User.PermissionId;
permissionId
将为您提供所需的ID。
答案 4 :(得分:0)
我将UrlFetchApp与Google Apps脚本一起使用,以替换Drive API v2和Advanced Drive Service(基于v2)。
使用公司域服务帐户,第getService(userEmail)
节使用库https://github.com/googleworkspace/apps-script-oauth2代表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('YourEmail@CompanyDomain.com') }
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;
};
}