无法使用仅元数据'用于发送消息的URI

时间:2015-03-11 17:58:14

标签: rest gmail-api

我正在尝试复制页面底部的API Explorer的功能: https://developers.google.com/gmail/api/v1/reference/users/messages/send 我已经想出如何创建base64编码的消息,并且可以让API Explorer发送消息,但我试图在“高级REST客户端”Chrome应用程序中复制结果。

我已将我的网址设为:

https://www.googleapis.com/gmail/v1/users/me/messages/send

我添加了以下标题:

Authorization: Bearer (valid Oauth2 token here)

Content-Type: message/rfc822

将身体设置为:

{"raw":"Q29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJ1cy1hc2NpaSINCk1JTUUtVmVyc2lvbjogMS4wDQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA3Yml0DQp0bzogYnJ5Y2VAdGhvcm1lZGlhLmNvbQ0KZnJvbTogYnJ5Y2VAdGhvcm1lZGlhLmNvbQ0Kc3ViamVjdDogVGVzdA0KDQpIZWxsbyE="}

当我发送请求时,我收到回复:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "wrongUrlForUpload",
    "message": "Uploads must be sent to the upload URL. Re-send this request to https://www.googleapis.com/upload/gmail/v1/users/me/messages/send"
   }
  ],
  "code": 400,
  "message": "Uploads must be sent to the upload URL. Re-send this request to https://www.googleapis.com/upload/gmail/v1/users/me/messages/send"
 }
}

我正在试图弄清楚如何形成我的请求以允许我使用元数据URI。 API的文档似乎几乎专注于上传URI,因此理解这一点有点粗糙。

1 个答案:

答案 0 :(得分:2)

我不太熟悉高级REST客户端,但有效的ajax请求将是:

$.ajax({
      type: "POST",
      url: "https://www.googleapis.com/gmail/v1/users/me/messages/send",
      contentType: "application/json",
      dataType: "json",
      beforeSend: function(xhr, settings) {
        xhr.setRequestHeader('Authorization','Bearer ' + accessToken);
      },
      data: JSON.stringify({"raw": mail}) // The url-safe base64 encoded mail

换句话说,我认为你使用的是错误的contentType;当你发送一个带有“原始”参数的对象时,它应该是“application / json”,而不是“message / rfc822”。