I use Javascript (in the Google Apps Script editor) to make HTTP requests to the Google Drive API.
I have successfully made individual API calls, so I know that my Oauth 2 works, but now I'm working on creating a batch request.
I will loop through each file and make 2 API calls per file.
My request:
var newOwnerEmail = 'archive@test.com';
var newEditorEmail = 'admin@test.com';
var fileIds = ['a', 'b', 'c'];
const boundary = 'testboundaryname';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var multipartRequestBody = '';
for (var i = 0; i < fileIds.length; i++) {
var fileId = fileIds[i];
var resource1 = { role: "owner", type: "user", value: newOwnerEmail };
var requestBody = {};
requestBody.path = '/drive/v2/files/' + fileId + '/permissions';
requestBody.method = 'POST';
requestBody.contentType = 'application/json';
requestBody.payload = JSON.stringify(resource1);
requestBody.muteHttpExceptions = true;
multipartRequestBody += delimiter;
multipartRequestBody += 'Content-Type: application/json\r\n';
multipartRequestBody += JSON.stringify(requestBody);
var resource2 = { role: "writer", type: "user", value: newEditorEmail };
requestBody.payload = JSON.stringify(resource2);
multipartRequestBody += delimiter;
multipartRequestBody += 'Content-Type: application/json\r\n';
multipartRequestBody += JSON.stringify(requestBody);
}
multipartRequestBody += close_delim;
var batchUrl = 'https://www.googleapis.com/batch';
var batchRequestBody = {};
batchRequestBody.headers = { Authorization: 'Bearer ' + service.getAccessToken() };
batchRequestBody.method = 'POST';
batchRequestBody.contentType = 'multipart/mixed; boundary="' + boundary + '"';
batchRequestBody.body = multipartRequestBody;
batchRequestBody.muteHttpExceptions = true;
try {
var batchResponse = UrlFetchApp.fetch(batchUrl, batchRequestBody);
Logger.log(batchResponse);
multipartRequestBody ends up looking like this:
--testboundary
Content-Type: application/json
{"method":"POST","contentType":"application/json","payload":"{\"role\":\"owner\",\"type\":\"user\",\"value\":\"archive@test.com\"}","muteHttpExceptions":false}
--testboundary
Content-Type: application/json
{"method":"POST","contentType":"application/json","payload":"{\"role\":\"writer\",\"type\":\"user\",\"value\":\"admin@test.com\"}","muteHttpExceptions":false}
--testboundary
Content-Type: application/json
{"method":"POST","contentType":"application/json","payload":"{\"role\":\"owner\",\"type\":\"user\",\"value\":\"archive@test.com\"}","muteHttpExceptions":false}
--testboundary
Content-Type: application/json
{"method":"POST","contentType":"application/json","payload":"{\"role\":\"writer\",\"type\":\"user\",\"value\":\"admin@test.com\"}","muteHttpExceptions":false}
--testboundary
Content-Type: application/json
{"method":"POST","contentType":"application/json","payload":"{\"role\":\"owner\",\"type\":\"user\",\"value\":\"archive@test.com\"}","muteHttpExceptions":false}
--testboundary
Content-Type: application/json
{"method":"POST","contentType":"application/json","payload":"{\"role\":\"writer\",\"type\":\"user\",\"value\":\"admin@test.com\"}","muteHttpExceptions":false}
--testboundary--
I ran UrlFetchApp.getRequest()to find out what was sent to the server, and this:
{
"headers": {
"Authorization": "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"X-Forwarded-For": "(our external IP address)"
},
"method": "post",
"payload": "\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"owner\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"archive@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"admin@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"owner\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"archive@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"admin@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"owner\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"archive@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"admin@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"owner\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"archive@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"admin@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"owner\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"archive@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"admin@test.com\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary--",
"followRedirects": true,
"validateHttpsCertificates": true,
"useIntranet": false,
"contentType": "multipart/mixed; boundary=\"testboundary\"",
"url": "https://www.googleapis.com/batch"
}
I get this answer:
Unsupported number format: --
There is definitely something wrong with the contents of the request, but it’s difficult to determine what it is, given that there is such little documentation for creating batch requests using Javascript.
, , - this, /.
, , , Google API, Script.