在新的Gmail API中批量提取电子邮件 [英] Bulk-fetching emails in the new Gmail API
问题描述
我使用的是Google新发布的Gmail API的Python版本。
$ b 以下调用仅返回消息ID列表:
service.users()。messages()。list(userId ='me')。execute()
但是我只是有一个消息ID列表,需要对它们进行迭代并逐个获取它们。
是否有一种方法可以在一次调用中获取整个消息内容以获取id列表?
(类似于Google Calendar API中的做法)?
如果还不支持,Google是否愿意考虑添加API?
更新
以下是适用于我的解决方案:
batch = BatchHttpRequest()
for message_ids中的msg_id:
batch.add(service.users()。messages()。get(userId ='me',id = msg_id [ id']),callback = mycallbackfunc)
batch.execute()
下面是Java中的批量请求示例,其中我使用线程ID获取所有线程。这可以很容易地适应您的需要。
BatchRequest b = service.batch();
//回调函数。 (也可以根据需要为每个请求定义不同的回调)
JsonBatchCallback< Thread> bc = new JsonBatchCallback< Thread>(){
@Override
public void onSuccess(Thread t,HttpHeaders responseHeaders)
throws IOException {
System.out.println (t.getMessages()得到(0).getPayload()getBody()的getData()。);
}
@Override
public void onFailure(GoogleJsonError e,HttpHeaders responseHeaders)
抛出IOException {
}
} ;
//对批处理请求排队请求
for(Thread thread:threads){
service.users()。threads()。get(me,threads。 getId())。queue(b,bc);
}
b.execute();
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()
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屋!