Search code examples
google-chromegoogle-chrome-extensiongmailgmail-apigoogle-api-js-client

Same thread id does not return the same message


Trying to retrieve only messages sent to me (and not my outgoing messages), in this case I have sent an email and it has been replied to. When I use the following request I get the thread with only the incoming message as desired:

"https://www.googleapis.com/gmail/v1/users/me/threads?q=-from%3Ame+in%3Ainbox&access_token=" + thisToken

However when I grab the message ID from the return and plug it into an individual message request:

"https://www.googleapis.com/gmail/v1/users/me/messages/" + messageId + "?access_token=" + thisToken

The message returned is the first message in the thread, which in this case is my outgoing message. I've demonstrated this below in the API Explorer. Is it possible to get the message IDs of only incoming messages? The fact that the Thread API Explorer was able to sort it out leads me to believe it is possible...

(I'm a newbie. Any help is sincerely appreciated, and I am grateful for your time.)


Thread API Explorer example with desired return

thread

Message API Explorer example with undesired return using same ID

thread


Solution

  • The id you are passing to message.get() call is the thread ID. This works because for the first message in a thread, thread ID and message ID are the same. For subsequent messages in a thread, they are different.

    You can call thread.list() with query to retrieve the thread IDs of interest and then call thread.get() to retrieve all the message IDs in a given thread. Then call message.get() for each of the retrieved message IDs and check for labelIds of each message. Exclude the ones which have labelId "SENT".