4

I'm able to use GMail's Users.threads.list API call to retrieve a list of the threads. I'd like to also grab the labels belonging to each message in the thread.

On the official documentation / live example for this method, there is an area called fields where the user can specify the optional fields they wish to pull in. They even provide a little UI tool call 'fields editor' to help you select and properly format the fields for inclusion in the REST query:

enter image description here

Which results in the following, valid, generated fields parameter values:

nextPageToken,resultSizeEstimate,threads(historyId,id,messages/labelIds)

The final request looks like this:

GET https://www.googleapis.com/gmail/v1/users/me/threads?includeSpamTrash=true&fields=nextPageToken%2CresultSizeEstimate%2Cthreads&key={YOUR_API_KEY} 

After authenticating with OAuth2, I get back a list of threads[], but none of them include the expected messages[] array nested within them! I know the field mask is valid because GMail API errors out when you request a field the method doesn't support with a standard HTTP 400 and a pretty printed error message. But in this case, all I get back is an object that contains an array labeled "threads", where each thread object only has the following fields:

  • id
  • snippet
  • historyId

But no messages array. Stranger yet, even when I remove the fields custom filter parameter, I still get back these same results. Am I missing something obvious here?

1 Answer 1

1

You have to list the id of the threads first, and then make a separate request to get the thread and fields in the messages you want. The API Explorer allows you to select additional fields when listing for some reason.

1. List ids

Request

GET https://www.googleapis.com/gmail/v1/users/me/threads?access_token={access_token} 

Response

{ "threads": [ { "id": "1570f8c16a1084de", "snippet": "Foo bar...", "historyId": "1234" }, ... ], "resultSizeEstimate": 100 } 

2. Get the thread

Request

GET https://www.googleapis.com/gmail/v1/users/me/threads/1570f8c16a1084de?access_token={access_token} 

Response

{ "id": "1570f8c16a1084de", "historyId": "1234", "messages": [ { "id": "1570f8c16a1084de", "threadId": "1570f8c16a1084de", "labelIds": [ "INBOX", "IMPORTANT", "CATEGORY_FORUMS" ], ... } } 

If you don't want to list threads and then get them all separately, you could use batch requests to bring it down to 2 requests instead.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.