使用 api 发送 Gmail 附件失败 [英] Sending gmail attachment using api failed
问题描述
我正在尝试使用其 API 创建带有 Gmail 附件的草稿(或发送邮件).我已经阅读了一些答案并尝试根据我在此处阅读的内容构建请求:邮件附件错误的媒体类型 Gmail API
在对函数本身进行编码之前,我决定使用 Chrome 扩展程序(Simple Rest Client)来模拟 API 请求.这是请求正文:
内容类型:multipart_mixed;边界=foo_bar_baz"MIME 版本:1.0至:receiver@gmail.com来自:sender@gmail.com科目:测试科目--foo_bar_baz内容类型:文本/纯文本;字符集=UTF-8"MIME 版本:1.0内容传输编码:7 位这是测试文本--foo_bar_baz内容类型:图像/jpegMIME 版本:1.0内容传输编码:base64Content-Disposition:附件;文件名=测试.jpg"{信息":{原始": _9j_4AAQSkZJRgABAQEAYABgAAD_2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz_2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz_wAARCAAJAAsDASIAAhEBAxEB_8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL_8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4-Tl5ufo6erx8vP09fb3-Pn6_8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL_8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3-Pn6_9oADAMBAAIRAxEAPwD9Pfiv-wN4q8cftk3Pji30_wCH9z9v8V6H4ksPiFe3cy-MvAunaeuni68N6bCLR92n3_2G8ErLf2yAeIL_AHW021xdfX9FFAH_2Q **"}}--foo_bar_baz--
请求头参数如下:
授权:不记名*给定令牌*内容类型:多部分/混合;边界=foo_bar_baz"内容长度:1428
如您所见,它与上面链接中的示例非常相似.但是,我不断收到以下回复:
"message": "不支持媒体类型‘application/octet-stream’.有效媒体类型:[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 媒体类型的情况下解决了他的问题.
我一定错过了一些东西.有人可以帮我弄这个吗?如果有人能帮我弄清楚,我真的很感激.
好的,所以如果您使用/upload 媒体功能(适用于所有消息,无论大小如何),那么例如它应该类似于以下内容(看起来我有点误会了):
<前>发布 https://www.googleapis.com/upload/gmail/v1/users/me/messages/send内容类型:多部分/相关;边界=foo_bar_baz那么您的 POST 正文应该类似于以下内容(未编码等):
<前>--foo_bar_baz内容类型:应用程序/json;字符集=UTF-8{}--foo_bar_baz内容类型:消息/rfc822MIME 版本:1.0至:receiver@gmail.com来自:sender@gmail.com科目:测试科目--foo_bar_baz内容类型:文本/纯文本;字符集=UTF-8"MIME 版本:1.0内容传输编码:7 位这是测试文本--foo_bar_baz内容类型:图像/jpegMIME 版本:1.0内容传输编码:base64Content-Disposition:附件;文件名=测试.jpg"--foo_bar_baz--所以需要注意的是,它实际上是多部分/相关的",并且有一个 application/json(对于某些请求,您可以在那里添加参数)部分以及包含整个电子邮件的 message/rfc822 部分.>
这确实不容易——如果你可以使用它们,图书馆肯定会减少痛苦!
I'm trying create a draft (or send a message) with attachment to gmail using its API. I've read some answers and tried to built the request according to what I've read here: Mail attachment wrong media type Gmail API
Before coding the function itself, I decided to use a Chrome extension (Simple Rest Client) to simulate the API request. Here's the request body:
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--
The request header parameters are as follows:
Authorization: Bearer *given token*
Content-Type: multipart/mixed; boundary="foo_bar_baz"
Content-Length: 1428
As you can see, it's pretty similar to the example in the link above. However, I keep getting the following response:
"message": "Media type 'application/octet-stream' is not supported. Valid media types: [message/rfc822]"
I know the API docs say the only valid media type is message/rfc822 (https://developers.google.com/gmail/api/v1/reference/users/drafts/create). Nonetheless, this sample (https://developers.google.com/gmail/api/guides/uploads#multipart) and others here in Stackoverflow say otherwise. The author of the question in the link above seem to have solved his problem without using message/rfc822 media type.
I gotta be missing something. Can someone help me with this? I'd really appreciate if someone could help me figure it out.
OK, so if you're using the /upload media feature (works for all messages irregardless of size) then for example it should be something like the following (and looks like i was a bit mistaken):
POST https://www.googleapis.com/upload/gmail/v1/users/me/messages/send Content-Type: multipart/related; boundary=foo_bar_baz
then your POST body should be something like the following (not encoded, etc):
--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--
So things to note are that it's actually "multipart/related" and that has a application/json (for some requests you can add parameters there) part as well as a message/rfc822 part that contains the entire email.
It's not easy for sure--libraries definitely make it less painful if you can use them!
这篇关于使用 api 发送 Gmail 附件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!