如何从Google Apps脚本发出Gmail API批处理请求? [英] How to make a Gmail API batch request from google apps script?

查看:89
本文介绍了如何从Google Apps脚本发出Gmail API批处理请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为示例,在以下情况下需要批处理请求: 使用Gmail.Users.Threads.list(..)之后,我想批量执行几个Gmail.Users.Threads.get(threadId,..)操作.

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.

我说的是与 javascript中的gapi.client.newBatch();调用类似的内容gmail api .

I am talking about something similar to gapi.client.newBatch(); call in javascript gmail api.

首先在应用程序脚本中,需要按照

First in apps script, one needs to enable the Gmail v1 Api in Advanced Google Services as described here.

然后在Google Apps脚本中使用Gmail Api如下所示:

Then using the Gmail Api in google apps script looks like this:

建议是:

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().

推荐答案

这个答案怎么样?我找不到Gmail.Users.Threads.get()的批处理请求的方法.而且,在Google Apps脚本中,没有用于请求批处理请求的方法.因此需要实现该方法.批处理请求的流程如下.

How about this answer? I couldn't find the method of batch request for Gmail.Users.Threads.get(). And at Google Apps Script, there are no methods for requesting the batch request. So it is required to implement the method. The flow of batch request is as follows.

  1. 为批处理请求创建请求正文.
  2. 使用multipart/mixed将主体重新设置为POST https://www.googleapis.com/batch的端点.
    • 仅此帖子需要使用访问令牌.
  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.
    • The access token is required to be used for only this post.

此流程的示例脚本如下.

The sample script for this flow is as follows.

  1. 使用Gmail.Users.Threads.list()检索线程列表.
  2. Gmail.Users.Threads.get()创建请求正文.
    • 在这种情况下,高级Google服务的Gmail.Users.Threads.get()无法使用,因此需要直接使用API​​.
  1. Retrieve thread list using Gmail.Users.Threads.list().
  2. Create the request body for 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.

脚本:

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

注意:

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆