0

I am trying to update a draft message with Google Apps Script. Here is the code.

 var forScope = GmailApp.getInboxUnreadCount();
 var params = {method:"put",
                contentType: "application/json",
                headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
                muteHttpExceptions:true,
                payload:JSON.stringify({
                  "message": {
                    "id": draftId,
                    "raw": draftBody
                  }
                })
               };
  var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts/"+draftId, params);

But it is returning following error

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"
   }
  ],
  "code": 404,
  "message": "Not Found"
 }
}

I have verified multiple times that the draft id is correct.

Hari Das
  • 10,145
  • 7
  • 62
  • 59

2 Answers2

2

AFAIK, draft ID is different from the message ID. Looking at your code, seems like you're supplying messageId instead of draftId.

Try obtaining the immutable draft ID using Users.drafts: list. This will return a response body with the following structure

"drafts":[
  {
  "id": draftId,
  "message": {
     "id": messageId,
     "raw": bytes
     }
  }
]

Then, you can use draftId when you update using Users.drafts: update.

Teyam
  • 7,686
  • 3
  • 15
  • 22
0

You need the uploads URL.

developers.google.com - Users.drafts: update | Gmail API | Google Developers

https://developers.google.com/gmail/api/v1/reference/users/drafts/update

Riël
  • 1,251
  • 1
  • 16
  • 31