使用 Javascript 的 Google Drive API 多部分/混合批量请求的正确格式

2024-03-24

我正在使用 Javascript(在 Google Apps 脚本编辑器中)向 Google Drive API 发出 HTTP 请求。

我已成功对 API 进行了单独调用,因此我知道我的 Oauth 2 可以正常工作,但现在我正在努力发出批量请求。

我将循环遍历每个文件,并对每个文件进行 2 个 API 调用。

我的请求:

var newOwnerEmail = '[email protected] /cdn-cgi/l/email-protection';
var newEditorEmail = '[email protected] /cdn-cgi/l/email-protection';
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最终成为一个如下所示的字符串:

 --testboundary
 Content-Type: application/json
 {"method":"POST","contentType":"application/json","payload":"{\"role\":\"owner\",\"type\":\"user\",\"value\":\"[email protected] /cdn-cgi/l/email-protection\"}","muteHttpExceptions":false}
 --testboundary
 Content-Type: application/json
 {"method":"POST","contentType":"application/json","payload":"{\"role\":\"writer\",\"type\":\"user\",\"value\":\"[email protected] /cdn-cgi/l/email-protection\"}","muteHttpExceptions":false}
 --testboundary
 Content-Type: application/json
 {"method":"POST","contentType":"application/json","payload":"{\"role\":\"owner\",\"type\":\"user\",\"value\":\"[email protected] /cdn-cgi/l/email-protection\"}","muteHttpExceptions":false}
 --testboundary
 Content-Type: application/json
 {"method":"POST","contentType":"application/json","payload":"{\"role\":\"writer\",\"type\":\"user\",\"value\":\"[email protected] /cdn-cgi/l/email-protection\"}","muteHttpExceptions":false}
 --testboundary
 Content-Type: application/json
 {"method":"POST","contentType":"application/json","payload":"{\"role\":\"owner\",\"type\":\"user\",\"value\":\"[email protected] /cdn-cgi/l/email-protection\"}","muteHttpExceptions":false}
 --testboundary
 Content-Type: application/json
 {"method":"POST","contentType":"application/json","payload":"{\"role\":\"writer\",\"type\":\"user\",\"value\":\"a[email protected] /cdn-cgi/l/email-protection\"}","muteHttpExceptions":false}
 --testboundary--

I ran UrlFetchApp.getRequest()查看发送到服务器的内容,如下所示:

{
  "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\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"owner\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"owner\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"owner\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"owner\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary\r\nContent-Type: application/json\r\n{\"method\":\"POST\",\"contentType\":\"application/json\",\"payload\":\"{\\\"role\\\":\\\"writer\\\",\\\"type\\\":\\\"user\\\",\\\"value\\\":\\\"[email protected] /cdn-cgi/l/email-protection\\\"}\",\"muteHttpExceptions\":false}\r\n--testboundary--",
  "followRedirects": true,
  "validateHttpsCertificates": true,
  "useIntranet": false,
  "contentType": "multipart/mixed; boundary=\"testboundary\"",
  "url": "https://www.googleapis.com/batch"
}

我得到这样的回应:

Unsupported number format: --

请求的内容肯定有问题,但很难确定问题是什么,因为使用 Javascript 创建批量请求的文档很少。

我能找到的唯一文档是this https://developers.google.com/drive/web/batch,但该示例与语言无关/假设。

然后我发现这个例子 https://developers.google.com/drive/web/quickstart/quickstart-js,它似乎使用了 Google API 客户端库,但我无法在 Apps 脚本中使用它。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Javascript 的 Google Drive API 多部分/混合批量请求的正确格式 的相关文章

随机推荐