在新的Gmail API中批量处理电子邮件

时间:2014-07-03 20:42:26

标签: python google-api gmail-api

我正在使用谷歌新发布的Gmail API的python版本。

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

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

但是我只有一个消息ID列表,需要迭代它们并逐个获取它们。

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

如果尚未支持,Google会考虑在API中添加这些内容吗?

更新

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

2 个答案:

答案 0 :(得分:18)

以下是Java中批处理请求的示例,其中我使用线程ID获取所有线程。这可以很容易地适应您的需要。

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();

答案 1 :(得分:11)

以下是适合我的解决方案:

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