Context:
I am trying to use the Google Drive API to remove my own access to files that have been shared with me. My goal is to stop having access to these files entirely. I am not the owner of these files, but I have been added as a collaborator (either as an editor, viewer, or commenter).
What I’ve Tried:
I’ve written a script using Google Apps Script that attempts to remove my permissions using the permissions.delete
method from the Google Drive API. Here’s the relevant portion of the script:
function removeMyselfFromAllFiles() {
const activeUserEmail = Session.getActiveUser().getEmail();
var pageToken = null;
do {
var result = Drive.Files.list({
q: "trashed = false",
pageSize: 100,
pageToken: pageToken,
corpora: "allDrives",
includeItemsFromAllDrives: true,
supportsAllDrives: true,
fields: "nextPageToken, files(id, name, permissions)"
});
if (result.files && result.files.length > 0) {
var files = result.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
for (var j = 0; j < file.permissions.length; j++) {
var permission = file.permissions[j];
if (permission.emailAddress === activeUserEmail) {
var url = 'https://www.googleapis.com/drive/v3/files/' + file.id + '/permissions/' + permission.id;
var response = UrlFetchApp.fetch(url, {
method: 'delete',
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken()
},
muteHttpExceptions: true
});
if (response.getResponseCode() === 204) {
Logger.log("Successfully removed myself from: " + file.name);
} else {
Logger.log("Failed to remove myself from: " + file.name + " - Response: " + response.getContentText());
}
}
}
}
}
pageToken = result.nextPageToken;
} while (pageToken);
}
The Problem:
When running the script, I often encounter the following error:
Failed to remove myself from: [File Name] - Response: {
"error": {
"code": 403,
"message": "The user does not have sufficient permissions for this file.",
"errors": [
{
"message": "The user does not have sufficient permissions for this file.",
"domain": "global",
"reason": "insufficientFilePermissions"
}
]
}
}
I understand that the error occurs because I don’t have sufficient permissions to modify the sharing settings of the file. However, my goal is simply to remove my access to the file so that it no longer appears in my Google Drive, and neither my email appears as a collaborator on the file.
My Questions:
- Is there a way to programmatically remove my access to a file when I am not the owner, using the Google Drive API?
- If not, are there any alternative approaches I could take to achieve the same goal (stopping access to the file) through the API or any other method?
- Could there be a different API method or strategy that I should be using instead?
Additional Context:
- I want to automate the process for multiple files and avoid manually changing permissions on each one.
- I’m looking for a scalable solution that can handle many files automatically without manual intervention.
Any help or guidance on how to achieve this would be greatly appreciated!