简体   繁体   English

如何通过Google Apps脚本发出Gmail API批处理请求?

[英]How to make a Gmail API batch request from google apps script?

As an example, I need the batch request in the following scenario: After using Gmail.Users.Threads.list(..) I would like to do several Gmail.Users.Threads.get(threadId,..) operations in a batch. 例如,在以下情况下,我需要批处理请求:使用Gmail.Users.Threads.list(..)我想在批处理中执行几个Gmail.Users.Threads.get(threadId,..)操作。

I am talking about something similar to gapi.client.newBatch(); 我在说类似于gapi.client.newBatch();东西gapi.client.newBatch(); call in javascript gmail api . 调用javascript gmail api

First in apps script, one needs to enable the Gmail v1 Api in Advanced Google Services as described here . 首先在应用程序脚本中,需要按此处所述在Advanced Google Services中启用Gmail v1 Api。

Then using the Gmail Api in google apps script looks like this: 然后在Google Apps脚本中使用Gmail Api如下所示: Google Apps脚本中的Gmail Api

The suggestions are: 建议是:

Users : UsersCollection
newAutoForwarding() : AutoForwarding
newBatchDeleteMessagesRequest() : BatchDeleteMessagesRequest
newBatchModifyMessagesRequest() : BatchModifyMessagesRequest
newDraft() : Draft
newFilter() : Filter
newFilterAction() : FilterAction
newFilterCriteria() : FilterCriteria
newForwardingAddress() : ForwardingAddress
newImapSettings() : ImapSettings
newLabel() : Label
newLabelColor() : LabelColor
newMessage() : Message
newMessagePart() : MessagePart
newMessagePartBody() : MessagePartBody
newMessagePartHeader() : MessagePartHeader
newModifyMessageRequest() : ModifyMessageRequest
newModifyThreadRequest() : ModifyThreadRequest
newPopSettings() : PopSettings
newSendAs() : SendAs
newSmimeInfo() : SmimeInfo
newSmtpMsa() : SmtpMsa
newVacationSettings() : VacationSettings
newWatchRequest() : WatchRequest

There is no newBatch() suggested. 没有建议newBatch()

How about this answer? 这个答案怎么样? I couldn't find the method of batch request for Gmail.Users.Threads.get() . 我找不到针对Gmail.Users.Threads.get()的批处理请求的方法。 And at Google Apps Script, there are no methods for requesting the batch request. 而且,在Google Apps脚本中,没有用于请求批处理请求的方法。 So it is required to implement the method. 因此需要实现该方法。 The flow of batch request is as follows. 批处理请求的流程如下。

  1. Create the request body for the batch request. 创建批处理请求的请求正文。
  2. Requst the body to the endpoint of POST https://www.googleapis.com/batch using multipart/mixed . 使用multipart/mixed将正文重新排列到POST https://www.googleapis.com/batch的端点。
    • The access token is required to be used for only this post. 要求访问令牌仅用于此帖子。

The sample script for this flow is as follows. 此流程的示例脚本如下。

Sample script : 示例脚本:

Flow : 流 :

  1. Retrieve thread list using Gmail.Users.Threads.list() . 使用Gmail.Users.Threads.list()检索线程列表。
  2. Create the request body for Gmail.Users.Threads.get() . Gmail.Users.Threads.get()创建请求正文。
    • In this case, Gmail.Users.Threads.get() of Advanced Google Services cannot be used, so it is required to directly use the API. 在这种情况下,无法使用Advanced Google Services的Gmail.Users.Threads.get() ,因此需要直接使用API​​。
  3. Post the created body using multipart/mixed . 使用multipart/mixed发布创建的multipart/mixed
  4. Parse the response. 解析响应。

Script : 剧本:

function myFunction() {
  var userId = "me"; // Please modify this, if you want to use other userId.
  var threadList = Gmail.Users.Threads.list(userId).threads;
  var body = threadList.map(function(e){
    return {
        method: "GET",
        endpoint: "https://www.googleapis.com/gmail/v1/users/" + userId + "/threads/" + e.id
    }
  });
  var url = "https://www.googleapis.com/batch";
  var boundary = "xxxxxxxxxx";
  var contentId = 0;
  var data = "--" + boundary + "\r\n";
  for (var i in body) {
    data += "Content-Type: application/http\r\n";
    data += "Content-ID: " + ++contentId + "\r\n\r\n";
    data += body[i].method + " " + body[i].endpoint + "\r\n\r\n";
    data += "--" + boundary + "\r\n";
  }
  var payload = Utilities.newBlob(data).getBytes();
  var options = {
    method: "post",
    contentType: "multipart/mixed; boundary=" + boundary,
    payload: payload,
    headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true,
  };
  var res = UrlFetchApp.fetch(url, options).getContentText();
  var dat = res.split("--batch");
  var result = dat.slice(1, dat.length - 1).map(function(e){return e.match(/{[\S\s]+}/g)[0]});

  Logger.log(result.length)
  Logger.log(result)
}

Note : 注意 :

  • The parsed response is an array. 解析的响应是一个数组。 Each element in the array is corresponding to each element in the request body. 数组中的每个元素都对应于请求正文中的每个元素。
  • In this sample script, the thread list is retrieved by Gmail.Users.Threads.list("me").threads . 在此示例脚本中,线程列表由Gmail.Users.Threads.list("me").threads检索。 If you want to use some threads, please modify the request body. 如果要使用某些线程,请修改请求正文。

Reference : 参考:

If I misunderstand your question, I'm sorry. 如果我误解了您的问题,对不起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM