Gmail Draft API returning 404 error

孤街浪徒 提交于 2019-12-12 04:46:58

问题


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



来源:https://stackoverflow.com/questions/44463669/gmail-draft-api-returning-404-error

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