Gmail草稿API返回404错误

时间:2017-06-09 17:30:08

标签: google-apps-script google-api gmail-api

我正在尝试使用Google Apps脚本更新草稿消息。这是代码。

 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);

但它正在返回以下错误

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

我已多次验证草稿ID是否正确。

2 个答案:

答案 0 :(得分:1)

AFAIK,import { Injectable } from '@angular/core'; import {Http, Response, Request, RequestOptions, RequestOptionsArgs, Headers} from '@angular/http'; import 'rxjs/add/operator/map'; const headers = new Headers({ 'Access-Token': 'My-Access-Token' }); let options = new RequestOptions({ headers: headers }); @Injectable() export class ServicesService { private searchUrl: string; constructor(private _http: Http) { } searchMusic(str: String, type='artist'){ this.searchUrl = 'https://api.spotify.com/v1/search?query='+str+'&offset=0&limit=20&type='+type+'&market=US' return this._http.get(this.searchUrl, options) .map(res=>res.json()); } } draft ID不同。查看代码,似乎您提供message ID而不是messageId

尝试使用Users.drafts: list获取不可变草稿ID。这将返回具有以下结构的响应正文

draftId

然后,您可以在使用Users.drafts: update进行更新时使用"drafts":[ { "id": draftId, "message": { "id": messageId, "raw": bytes } } ]

答案 1 :(得分:0)

您需要上传网址。

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

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