Gmail API all messages
Asked Answered
C

2

7

I need to get all messages in Inbox with gmail api. But I see only one way to do it.

  1. Get list of messages(id, threadID)

    GET https://www.googleapis.com/gmail/v1/users/somebody%40gmail.com/messages?labelIds=INBOX&key={YOUR_API_KEY}
    
  2. With id`s get all messages in loop

    While 
        GET https://www.googleapis.com/gmail/v1/users/somebody%40gmail.com/messages/147199d21bbaf5a5?key={YOUR_API_KEY}
    End of While
    

But for this way needed 100500 request. Have anybody idea how to get with one request all messages(or just payload field)?

Chrysolite answered 9/7, 2014 at 16:22 Comment(2)
Yo, which key in the Credentials did you use for your API KEY? I cannot figure out which key should I use. I keep on getting error 401Kat
Sorry! I do not remember! You can use just their library developers.google.com/gmail/api/quickstart/pythonChrysolite
B
20

Use batch and request 100 messages at a time. You will need to make 1000 requests but the good news is that's quite fine and it'll be easier for everyone (no downloading 1GB response in a single request!).

Documented at: https://developers.google.com/gmail/api/guides/batch

There's a few other people that have asked about batching Gmail Api here on Stack Overflow, so just do a quick search to find answers and examples.

Bice answered 9/7, 2014 at 17:33 Comment(0)
L
5

The approach you are doing is correct, as there is no 'GetAll' API to download them

Reasons include:

Unbounded Result Sets

Pulling out an unlimited amount of emails (aka unbounded result set) is a resource hog on Google servers. Did you want the attachments AND images? These could be gigabytes of data.

Network Problems

Google has to read gigabytes form disk, store it in memory and send them over the internet. Google's server would handle it, but the bandwidth of internet connectivity would not work. Worst of all, if you issue this request again and again, you could perform a DDoS attack on Google.

Security Risk

If someone gains an API key of another user, they could download their entire mailbox. Hence Google provide paging to ensure that they can provide a securer service and reduce resource contention.

Therefore, it is there to protect you and other users, and themselves.

Lemire answered 9/7, 2014 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.