【问题标题】:How to make a Gmail API batch request from google apps script?如何从谷歌应用脚​​本发出 Gmail API 批处理请求?
【发布时间】:2018-08-13 17:19:08
【问题描述】:

例如,我在以下场景中需要批处理请求: 使用Gmail.Users.Threads.list(..) 后,我想批量执行几个Gmail.Users.Threads.get(threadId,..) 操作。

我说的是类似于gapi.client.newBatch(); 调用javascript gmail api 的东西。

首先在应用程序脚本中,需要在高级 Google 服务中启用 Gmail v1 Api,如 here 所述。

然后在谷歌应用脚​​本中使用 Gmail Api 如下所示:

建议是:

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

没有建议newBatch()

【问题讨论】:

  • 我的回答对您有用吗?如果您在我的回答中有问题,请随时告诉我。这些信息有助于我学习。另外我认为它可以帮助其他用户了解情况作为信息。

标签: javascript google-apps-script gmail-api


【解决方案1】:

这个答案怎么样?我找不到Gmail.Users.Threads.get() 的批量请求方法。在 Google Apps Script 中,没有请求批处理请求的方法。所以需要实现该方法。批量请求的流程如下。

  1. 为批处理请求创建请求正文。
  2. 使用multipart/mixed 将正文请求到POST https://www.googleapis.com/batch 的端点。
    • 访问令牌只能用于此帖子。

此流程的示例脚本如下。

示例脚本:

流程:

  1. 使用Gmail.Users.Threads.list()检索线程列表。
  2. Gmail.Users.Threads.get() 创建请求正文。
    • 此时无法使用Advanced Google Services的Gmail.Users.Threads.get(),需要直接使用API​​。
  3. 使用multipart/mixed发布创建的正文。
  4. 解析响应。

脚本:

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)
}

注意:

  • 解析的响应是一个数组。数组中的每个元素都对应于请求正文中的每个元素。
  • 在此示例脚本中,线程列表由Gmail.Users.Threads.list("me").threads 检索。如果您想使用某些线程,请修改请求正文。

参考:

如果我误解了你的问题,我很抱歉。

【讨论】:

猜你喜欢
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
相关资源
最近更新 更多