如何从Google Apps脚本发出Gmail API批处理请求? [英] How to make a Gmail API batch request from google apps script?
问题描述
作为示例,在以下情况下需要批处理请求:
使用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.
- 为批处理请求创建请求正文.
- 使用
multipart/mixed
将主体重新设置为POST https://www.googleapis.com/batch
的端点.- 仅此帖子需要使用访问令牌.
- Create the request body for the batch request.
- Requst the body to the endpoint of
POST https://www.googleapis.com/batch
usingmultipart/mixed
.- The access token is required to be used for only this post.
此流程的示例脚本如下.
The sample script for this flow is as follows.
- 使用
Gmail.Users.Threads.list()
检索线程列表. - 为
Gmail.Users.Threads.get()
创建请求正文.- 在这种情况下,高级Google服务的
Gmail.Users.Threads.get()
无法使用,因此需要直接使用API.
- 在这种情况下,高级Google服务的
- Retrieve thread list using
Gmail.Users.Threads.list()
. - 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.
- In this case,
脚本:
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
检索.如果要使用某些线程,请修改请求正文. - 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
. If you want to use some threads, please modify the request body.
Note :
如果我误解了你的问题,对不起.
If I misunderstand your question, I'm sorry.
这篇关于如何从Google Apps脚本发出Gmail API批处理请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!