Bulk-fetching emails in the new Gmail API

不羁的心 提交于 2019-12-17 16:27:15

问题


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

The following call returns just a list of message ids:

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

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

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) ?

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

Update

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


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/24562981/bulk-fetching-emails-in-the-new-gmail-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!