Hari Das
Hari Das

Reputation: 10849

Gmail Draft API returning 404 error

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.

Upvotes: 0

Views: 464

Answers (2)

Teyam
Teyam

Reputation: 8082

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.

Upvotes: 2

Riël
Riël

Reputation: 1321

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

Upvotes: 0

Related Questions