Gmail Draft API returning 404 error

2019-08-19 01:09发布

问题:

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.

回答1:

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.



回答2:

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