我正在尝试使用其API创建附加到gmail的草稿(或发送消息)。我已经阅读了一些答案,并尝试根据我在此处阅读的内容构建请求:Mail attachment wrong media type Gmail API
在对函数本身进行编码之前,我决定使用Chrome扩展程序(Simple Rest Client)来模拟API请求。这是请求正文:
Content-Type: multipart_mixed; boundary="foo_bar_baz"
MIME-Version: 1.0
to: receiver@gmail.com
from: sender@gmail.com
subject: Testing Subject
--foo_bar_baz
Content-Type: text/plain; charset="UTF-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
This is the testing text
--foo_bar_baz
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.jpg"
{
"message":
{
"raw" : "_9j_4AAQSkZJRgABAQEAYABgAAD_2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz_2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz_wAARCAAJAAsDASIAAhEBAxEB_8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL_8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4-Tl5ufo6erx8vP09fb3-Pn6_8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL_8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3-Pn6_9oADAMBAAIRAxEAPwD9Pfiv-wN4q8cftk3Pji30_wCH9z9v8V6H4ksPiFe3cy-MvAunaeuni68N6bCLR92n3_2G8ErLf2yAeIL_AHW021xdfX9FFAH_2Q**"
}
}
--foo_bar_baz--
请求标头参数如下:
Authorization: Bearer *given token*
Content-Type: multipart/mixed; boundary="foo_bar_baz"
Content-Length: 1428
正如您所看到的,它与上面链接中的示例非常相似。但是,我一直得到以下回复:
"message": "Media type 'application/octet-stream' is not supported. Valid media types: [message/rfc822]"
我知道API文档说唯一有效的媒体类型是message / rfc822(https://developers.google.com/gmail/api/v1/reference/users/drafts/create)。尽管如此,此示例(https://developers.google.com/gmail/api/guides/uploads#multipart)和Stackoverflow中的其他示例则另有说法。上面链接中的问题的作者似乎已经解决了他的问题,而没有使用message / rfc822媒体类型。
我得错过一些东西。有人可以帮我弄这个吗?如果有人能帮助我搞清楚,我真的很感激。
答案 0 :(得分:2)
好的,所以如果您正在使用/上传媒体功能(适用于所有不考虑大小的消息),那么例如它应该类似于以下内容(看起来我有点误):
POST https://www.googleapis.com/upload/gmail/v1/users/me/messages/send Content-Type: multipart/related; boundary=foo_bar_baz
那么你的POST主体应该类似于以下内容(未编码等):
--foo_bar_baz Content-Type: application/json; charset=UTF-8 { } --foo_bar_baz Content-Type: message/rfc822 MIME-Version: 1.0 to: receiver@gmail.com from: sender@gmail.com subject: Testing Subject --foo_bar_baz Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit This is the testing text --foo_bar_baz Content-Type: image/jpeg MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="test.jpg" --foo_bar_baz--
所以需要注意的是,它实际上是" multipart / related"并且有一个application / json(对于某些请求,你可以在那里添加参数)部分以及包含整个电子邮件的消息/ rfc822部分。
这并不容易确定 - 如果您可以使用它们,图书馆肯定会减少痛苦!