Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

google api - Sending gmail attachment using api failed

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: [email protected]
from: [email protected]
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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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: [email protected]
from: [email protected]
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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...