I've been utilizing the Google Drive API within Google Apps Script. This particular script is written in sandboxed server-side Javascript, which restricts the use of the Google Client API for Javascript.
As a Google Apps super admin, I am authenticating as the user through Domain-wide delegation and a service account to impersonate other users successfully making Files:get calls on behalf of different users within the domain.
Currently, my goal is to transfer ownership of a file from user A to user B while also granting user C editing permission by following these steps:
When authenticated as user A, transfer ownership of the file to user B by adding them as an owner.
While authentiated as user A or B, make user C an editor of the file.
I have already completed step 1 successfully.
For transferring ownership (Step 1), I believe I need to utilize the Permissions:insert method. I have drafted the following code snippet:
var url = 'https://www.googleapis.com/drive/v2/files/' + fileId + '/permissions';
var resource = {
"type": "user",
"role": "owner",
"value": "userBEmail"
};
var requestBody = {};
requestBody.method = 'POST';
requestBody.headers = { Authorization: 'Bearer ' + service.getAccessToken() };
requestBody.contentType = 'application/json';
requestBody.resource = JSON.stringify(resource);
requestBody.muteHttpExceptions = true;
try {
var response = UrlFetchApp.fetch(url, requestBody);
var result = JSON.parse(response.getContentText());
Logger.log('result: ' + JSON.stringify(result, null, 2));
} catch(e) {
Logger.log(e.message);
}
The returned result displays the following error message:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Permission type field required",
"locationType": "other",
"location": "permission.type"
}
],
"code": 400,
"message": "Permission type field required"
}
}
In light of this, I have several inquiries:
Is the approach taken suitable for transferring file ownership?
Why does it state
"Permission type field required"
when I have included it in the request?Upon accomplishing Step 1, is there a way to integrate Step 2 within the same API call? This would enhance performance especially if processing up to 1,000 files.
It's also noteworthy that I was able to effectively transfer ownership using the API explorer located at the bottom of this page, transitioning a file between two accounts with identical file IDs and request body content.