gpt4 book ai didi

java - 如何使用 Gmail Send API 的批量请求

转载 作者:行者123 更新时间:2023-12-02 12:41:11 30 4
gpt4 key购买 nike

我目前使用 Gmail API 一次发送多封电子邮件。我按顺序执行此操作:

  1. 通过 GmailAPI 发送电子邮件
  2. 等待 GmailAPI 的响应。
  3. 收到响应后,使用 GmailAPI 返回的 ThreadID 更新记录。
  4. 对其他电子邮件重复步骤 1-3。

我正在阅读有关批量发送电子邮件 API 请求的信息,以便我们对 API 进行一次调用并处理响应。虽然我可以批量处理我的所有请求并发送它,

我不确定如何处理回复。如果我在批量请求中有 30 个发送电子邮件请求,当收到该批处理的响应时,我如何确定哪个响应是针对哪个电子邮件的?

这是我的实现

BatchRequest batch  = gmailService.batch();
gmailService.users().messages().send("me", message).queue(batch, callback);
batch.execute();

final List<Message> messages = new ArrayList<Message>();
JsonBatchCallback<Message> callback = new JsonBatchCallback<Message>() {
public void onSuccess(Message message, HttpHeaders responseHeaders) {
System.out.println("MessageThreadID:"+ message.getThreadId());
System.out.println("MessageID:"+ message.getId());
synchronized (messages) {
messages.add(message);
}
}

@Override
public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders)
throws IOException {
}
};

最佳答案

I am not sure about how to handle Responses. If i have 30 Send Email Requests in the batch request, when the response is received for the batch, how do i figure out which response is for which Email?

根据这个Batch Request Response文档:

Response to a batch request

The server's response is a single standard HTTP response with a multipart/mixed content type; each part is the response to one of the requests in the batched request, in the same order as the requests.

Like the parts in the request, each response part contains a complete HTTP response, including a status code, headers, and body. And like the parts in the request, each response part is preceded by a Content-Type header that marks the beginning of the part.

您可以按照您发出请求的顺序获取完整的 HTTP 响应、其状态代码、 header 和正文。因此,响应 1 用于第一个请求,响应 2 用于第二个请求,依此类推。通过这种方式,您可能会计算出哪个电子邮件的响应。

关于java - 如何使用 Gmail Send API 的批量请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44927841/

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