gpt4 book ai didi

javascript - 如何从 Google Apps 脚本发出 Gmail API 批量请求?

转载 作者:行者123 更新时间:2023-12-03 02:21:55 26 4
gpt4 key购买 nike

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

我正在谈论类似于 javascript gmail api 中的 gapi.client.newBatch(); 调用。

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

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

建议是:

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 脚本中,没有用于请求批量请求的方法。所以需要实现该方法。批量请求流程如下。

  1. 为批量请求创建请求正文。
  2. 使用 multipart/mixed 将正文请求到 POST https://www.googleapis.com/batch 的端点。
    • 仅此帖子需要使用访问 token 。

此流程的示例脚本如下。

示例脚本:

流程:

  1. 使用 Gmail.Users.Threads.list() 检索线程列表。
  2. Gmail.Users.Threads.get() 创建请求正文。
    • 这种情况下,高级Google服务的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 检索。如果您想使用某些线程,请修改请求体。

引用:

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

关于javascript - 如何从 Google Apps 脚本发出 Gmail API 批量请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49120851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com