Gmail API finds parent of draft
How do I find the parent of a project (if it exists) in Gmail?
When a user replies to a specific message in the Gmail stream, a draft appears right below it. Let's say for example there were 5 posts in the thread and a project was done in response to a third ... this project will always show up as the 4th post. I'm trying to reproduce this in my application, but I need a way to map the draft to the message it replies, if any.
I've searched for return values but can't find a link to previous posts.
How can I do this via the API?
Edit:
source to share
You have to add the draft to the same thread and make sure the titles are References
both In-Reply-To
standard RFC 2822
and consistent with the titles Subject
.
Example
I have a thread with 3 posts. I want to create a draft that will be the answer to the second one. I get headlines Subject
, Message-ID
, References
, In-Reply-To
message, to which I would like to respond, and threadId
flow:
Request
GET https://www.googleapis.com/gmail/v1/users/me/messages/15a7a79ed814d9ec?format=metadata&metadataHeaders=Subject&metadataHeaders=Message-ID&metadataHeaders=References&metadataHeaders=In-Reply-To&access_token={access_token}
answer
{
"id": "15a7a79ed814d9ec",
"threadId": "15a7a79d389926b3",
"labelIds": [
"UNREAD",
"IMPORTANT",
"SENT",
"INBOX"
],
"snippet": "Test 2 2017-02-26 13:51 GMT+01:00 Emil Tholin <emtholin@gmail.com>: Test 1",
"historyId": "1138108",
"internalDate": "1488113495000",
"payload": {
"mimeType": "multipart/alternative",
"headers": [
{
"name": "In-Reply-To",
"value": "\u003cCADsZLRzQ8UQ1HJ8=YsvRv-jtpRY=s_wZmbL8RzSbCtw4T5A+vg@mail.gmail.com\u003e"
},
{
"name": "References",
"value": "\u003cCADsZLRzQ8UQ1HJ8=YsvRv-jtpRY=s_wZmbL8RzSbCtw4T5A+vg@mail.gmail.com\u003e"
},
{
"name": "Message-ID",
"value": "\u003cCADsZLRzHC_sR6THger6gkDjJ348XbXehQ0YsFwHAh762ht216A@mail.gmail.com\u003e"
},
{
"name": "Subject",
"value": "Re: Test"
}
]
},
"sizeEstimate": 1333
}
Then I just create a draft, base64 protected url encoded it and create in a stream:
// replace '+' with '-', replace '/' with '_', remove trailing '=' to make it url-safe
var draft = btoa([
'In-Reply-To: <CADsZLRzHC_sR6THger6gkDjJ348XbXehQ0YsFwHAh762ht216A@mail.gmail.com>\r\n',
'References: <CADsZLRzQ8UQ1HJ8=YsvRv-jtpRY=s_wZmbL8RzSbCtw4T5A+vg@mail.gmail.com> <CADsZLRzHC_sR6THger6gkDjJ348XbXehQ0YsFwHAh762ht216A@mail.gmail.com>\r\n',
'From: emtholin@gmail.com\r\n',
'To: emtholin@gmail.com\r\n',
'Subject: Re: Test\r\n'
].join('')).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
Request
POST https://www.googleapis.com/upload/gmail/v1/users/me/drafts?access_token={access_token}
{
"message": {
"raw": "SW4tUmVwbHktVG86IDxDQURzWkxSekhDX3NSNlRIZ2VyNmdrRGpKMzQ4WGJYZWhRMFlzRndIQWg3NjJodDIxNkFAbWFpbC5nbWFpbC5jb20-DQpSZWZlcmVuY2VzOiA8Q0FEc1pMUnpROFVRMUhKOD1Zc3ZSdi1qdHBSWT1zX3dabWJMOFJ6U2JDdHc0VDVBK3ZnQG1haWwuZ21haWwuY29tPiA8Q0FEc1pMUnpIQ19zUjZUSGdlcjZna0RqSjM0OFhiWGVoUTBZc0Z3SEFoNzYyaHQyMTZBQG1haWwuZ21haWwuY29tPg0KRnJvbTogZW10aG9saW5AZ21haWwuY29tDQpUbzogZW10aG9saW5AZ21haWwuY29tDQpTdWJqZWN0OiBSZTogVGVzdA0K",
"threadId": "15a7a79d389926b3"
}
}
Result
source to share
You can try using a method Users.drafts: list
that lists the drafts in the user's mailbox.
Example HTTP request:
GET https://www.googleapis.com/gmail/v1/users/userId/drafts
If successful, it will return the threadId
parent draft message.
{
"drafts": [
{
"id": "s:-ID",
"message": {
"id": "ID",
"threadId": "ID"
}
}
],
"resultSizeEstimate": 1
}
Hope this helps!
source to share