发送使用API​​ Gmail附件失败 [英] Sending gmail attachment using api failed

查看:293
本文介绍了发送使用API​​ Gmail附件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个草案(或发送消息)带附件使用其API到Gmail。我读过一些答案,并试图建立了符合我在这里读的请求:的邮件附件错误的介质类型的Gmail API

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

编码函数本身之前,我决定用一个Chrome扩展(简单的REST客户端)来模拟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

正如你所看到的,这是类似于上面的链接的例子pretty。不过,我不断收到以下响应:

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]"

我认识的API文档说唯一有效的媒体类型消息/ RFC822(的 https://developers.google.com/gmail/api/v1/reference/users/drafts/create )。尽管如此,这个样本( https://developers.google.com/gmail/api/导游/上传#多部分)等人在这里,否则#1说。在上面的链接问题的作者似乎已经解决了他的问题,而无需使用消息/ 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.

我要失去了一些东西。有人可以帮助我?我真的AP preciate如果有人可以帮助我找到答案。

I gotta be missing something. Can someone help me with this? I'd really appreciate if someone could help me figure it out.

推荐答案

确定,因此,如果您使用的是/上传媒体功能(适用于大小的所有邮件irregardless),那么例如,它应该像下面的(看起来像我有点弄错):

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

那么你的POST主体应该是类似以下内容(不带codeD等):

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

所以事情需要注意的是,它实际上是多部分/相关,并具有应用程序/ JSON(对于某些请求,您可以添加参数存在)的一部分,以及包含整个电子邮件消息/ RFC822部分。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆