[英]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如下所示:
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.
批处理请求的流程如下。
POST https://www.googleapis.com/batch
using multipart/mixed
. multipart/mixed
将正文重新排列到POST https://www.googleapis.com/batch
的端点。
The sample script for this flow is as follows. 此流程的示例脚本如下。
Gmail.Users.Threads.list()
. Gmail.Users.Threads.list()
检索线程列表。 Gmail.Users.Threads.get()
. Gmail.Users.Threads.get()
创建请求正文。
Gmail.Users.Threads.get()
of Advanced Google Services cannot be used, so it is required to directly use the API. Gmail.Users.Threads.get()
,因此需要直接使用API。 multipart/mixed
. multipart/mixed
发布创建的multipart/mixed
。 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
. Gmail.Users.Threads.list("me").threads
检索。 If you want to use some threads, please modify the request body. If I misunderstand your question, I'm sorry. 如果我误解了您的问题,对不起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.