Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript的Google Drive API多部分/混合批处理请求的正确格式_Javascript_Google Apps Script_Google Api_Httprequest_Multipart - Fatal编程技术网

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

使用Javascript的Google Drive API多部分/混合批处理请求的正确格式,javascript,google-apps-script,google-api,httprequest,multipart,Javascript,Google Apps Script,Google Api,Httprequest,Multipart,我正在使用Javascript(在GoogleApps脚本编辑器中)向GoogleDriveAPI发出HTTP请求 我已经成功地对API进行了单独的调用,所以我知道我的Oauth 2可以工作,但现在我正在进行批处理请求 我将循环遍历每个文件,并对每个文件进行2次API调用 我的请求: var newOwnerEmail = 'archive@test.com'; var newEditorEmail = 'admin@test.com'; var fileIds = ['a', 'b', 'c'

我正在使用Javascript(在GoogleApps脚本编辑器中)向GoogleDriveAPI发出HTTP请求

我已经成功地对API进行了单独的调用,所以我知道我的Oauth 2可以工作,但现在我正在进行批处理请求

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

我的请求:

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);
我运行了
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\\\":\\\"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"
}
我得到的答复是:

Unsupported number format: --
请求的内容肯定有问题,但很难确定它是什么,因为使用Javascript创建批处理请求的文档太少了

我能找到的唯一文档是,但示例是独立于语言的/假设的


然后我发现,它似乎使用了一个GoogleAPI客户端库,我不能在应用程序脚本中使用它

哎呀!抱歉,我不知道建议的编辑是什么,然后我看到它被拒绝了,但我意外地批准了它。我添加了javascript标记,因为我觉得在本例中澄清我使用的是javascript很重要,因为googleapi的官方文档通常只有Python、Ruby、Java等的示例代码,但很少有javascript。在我的情况下,我需要能够使用Javascript,我很难解决这个问题,因为在互联网上几乎没有关于我尝试做什么的例子或类似问题;显示UrlFetchApp.getRequest的输出?输出在上面-我写下“我得到这个响应:”(错误请求
错误400
)的部分,我指的是getRequest结果。它会显示你发出的请求的详细信息。我不需要再弄清楚这一点,谷歌今天刚刚发布了一个。
Unsupported number format: --