Ajinkya Kathe
Ajinkya Kathe

Reputation: 52

How to get thread id from gmail for a certain email

I want to find the thread id for a certain email which I have sent from gmail. I went through the Gmail apis, in which there are methods to get the list of threads as well as a method which gives you all the messages related to that thread after you send the user id i.e email and thread id. I can use the list method to get thread id but is there any alternate way to get the thread id?

Upvotes: 3

Views: 2823

Answers (1)

Tholle
Tholle

Reputation: 112787

If you just have the messageId, you could get the message, specifically asking just for the threadId of that message:

Request

usedId = me
id = 1514453c0800d5fa
fields = threadId

GET https://www.googleapis.com/gmail/v1/users/me/messages/1514453c0800d5fa?fields=threadId&access_token={YOUR_API_KEY}

Response

{
 "threadId": "1514453c0800d5fa"
}

Then, just use this to get the thread:

Request

userId = me
id = 1514453c0800d5fa

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

Also, if you know the message is the first message in the thread, the threadId will be the same as the messageId, as in the example above.

If you don't have Google's own messageId but the Message-ID, you have to list the message as you said:

Request

userId = me
q = rfc822msgid:<Message-ID>

GET https://www.googleapis.com/gmail/v1/users/me/messages?q=rfc822msgid%3A%3C0000015148b39f7a-64856e69-3b6e-4e37-bff0-db4e26aae420-000000%40eu-west-1.amazonses.com%3E&access_token={YOUR_API_KEY}

Response

{
 "messages": [
  {
   "id": "15148b3a2f9a26a0",
   "threadId": "15148b3a2f9a26a0"
  }
 ],
 "resultSizeEstimate": 1
}

Upvotes: 2

Related Questions