Search code examples
ruby-on-railsgoogle-apigmail-apigoogle-api-client

Impossible to get messages details in a list request with Gmail API


I am using a ruby on rails app which connects to the Gmail API. When I make a listrequest to get all the messages of one mailbox, I only get back an idand a threadId property for each message.

I tried to follow Gmail API Doc using the fields parameters to get other properties (title, date...). It doesn't work, whether I use the google-api-client gem in my app, or by doing a direct GET request.

Adding any other parameters to the request ends with a failure. Here is the url that works : https://www.googleapis.com/gmail/v1/users/me/messages?fields=messages(id,threadId)

Am I forced to make one call per message or using batch requests to get relevant datas ? It seems a little heavy...


Solution

  • You first need to list messages like you've done, and then get each message in a separate request.

    Request 1

    GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=1&access_token={ACCESS_TOKEN}
    

    Response 1

    {
     "messages": [
      {
       "id": "15fd9f0fe242f975",
       "threadId": "15fd9f0fe242f975"
      }
     ],
     "nextPageToken": "11889180580605610074",
     "resultSizeEstimate": 2
    }
    

    Request 2

    GET https://www.googleapis.com/gmail/v1/users/me/messages/15fd9f0fe242f975?access_token={ACCESS_TOKEN}
    

    Response 2

    {
     "id": "15fd9f0fe242f975",
     "threadId": "15fd9f0fe242f975",
     "labelIds": [
      "IMPORTANT",
      "CATEGORY_UPDATES",
      "INBOX"
     ],
     "snippet": "Tasks tracked last week...",
     "historyId": "966691",
     ...
    }
    

    It's also possible to get the total amount of request down from 1 + n of messages to 2 by using batch requests.