问题
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.
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.
Then using the Gmail Api in google apps script looks like this:
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.
回答1:
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.
- 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.
Sample script :
Flow :
- 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,
- Post the created body using
multipart/mixed
. - Parse the response.
Script :
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)
}
Note :
- 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.
Reference :
- Batching Requests
If I misunderstand your question, I'm sorry.
来源:https://stackoverflow.com/questions/49120851/how-to-make-a-gmail-api-batch-request-from-google-apps-script