在新的 Gmail API 中批量获取电子邮件 [英] Bulk-fetching emails in the new Gmail API

查看:65
本文介绍了在新的 Gmail API 中批量获取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Google 新发布的 Gmail API 的 Python 版本.

I'm using the python version of the newly released Gmail API by Google.

以下调用仅返回消息 ID 列表:

The following call returns just a list of message ids:

service.users().messages().list(userId = 'me').execute()

但是我只有一个消息 ID 列表,需要遍历它们并一个一个地获取它们.

But then I just have a list of message ids and need to iterate over them and fetch them one-by-one.

有没有办法在一次调用中获取 id 列表的整个消息内容?(类似于在 Google Calendar API 中的实现方式)?

Is there a way to get the whole message content for a list of ids, in a single call ? (Similar to how it's done in the Google Calendar API) ?

如果尚不支持,Google 会考虑在 API 中添加吗?

And if not supported yet, is this something that Google would like to consider adding in the API ?

这是对我有用的解决方案:
batch = BatchHttpRequest()对于 message_ids 中的 msg_id:batch.add(service.users().messages().get(userId = 'me', id = msg_id['id']), callback = mycallbackfunc)batch.execute()

Here is the solution that worked for me:
batch = BatchHttpRequest() for msg_id in message_ids: batch.add(service.users().messages().get(userId = 'me', id = msg_id['id']), callback = mycallbackfunc) batch.execute()

推荐答案

这是 Java 中的批处理请求示例,我使用线程 ID 获取所有线程.这可以轻松适应您的需要.

Here is an example of batch request in Java where I get all the threads using threads ids. This can be easily adapted for your need.

BatchRequest b = service.batch();
//callback function. (Can also define different callbacks for each request, as required)
JsonBatchCallback<Thread> bc = new JsonBatchCallback<Thread>() {

    @Override
    public void onSuccess(Thread t, HttpHeaders responseHeaders)
            throws IOException {
        System.out.println(t.getMessages().get(0).getPayload().getBody().getData());
    }

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

    }
};

// queuing requests on the batch requests
for (Thread thread : threads) {
    service.users().threads().get("me", threads.getId()).queue(b, bc);
}


b.execute();

这篇关于在新的 Gmail API 中批量获取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆