Gmail API find parent of a draft

核能气质少年 提交于 2019-12-10 10:45:17

问题


How can I find the parent of a draft (if it exists) in Gmail?

When the user responds to a particular message in a Gmail Thread, the draft appears directly below it. Say for example, there was 5 messages in a thread and a draft was made in reply to the 3rd one... that draft would always appear as the 4th message. I'm attempting to reproduce this in my application, but I need a way of mapping a draft to the message it is in response to if any.

I've been searching through the values returned, but cannot find any reference to previous messages.

How can I do this through the API?

Edit:


回答1:


You have to add the draft to the same thread, and make sure the References and In-Reply-To headers are in compliance with the RFC 2822 standard, and that the Subject headers match.

Example

I have a thread with 3 messages. I want to create a draft that will be a response to the second one. I get the Subject, Message-ID, References, In-Reply-To headers of the message I would like to respond to, and the threadId of the thread:

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}

Response

{
 "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 make a draft, url-safe base64 encode it, and create it in the thread:

// 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




回答2:


You may try to use the Users.drafts: list method which lists the drafts in the user's mailbox.

Sample HTTP request: GET https://www.googleapis.com/gmail/v1/users/userId/drafts

If successful, it will return the threadId of the parent message of the draft.

{
 "drafts": [
  {
   "id": "s:-ID",
   "message": {
    "id": "ID",
    "threadId": "ID"
   }
  }
 ],
 "resultSizeEstimate": 1
}

Hope this helps!



来源:https://stackoverflow.com/questions/42448589/gmail-api-find-parent-of-a-draft

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!