2

I've tried to use the new Gmail api to download an image attachment from a particular email message part. (https://developers.google.com/gmail/api/v1/reference/users/messages/attachments#resource).

The message part is:

{u'mimeType': u'image/png', u'headers': {u'Content-Transfer-Encoding': [u'base64'], u'Content-Type': [u'image/png; name="Screen Shot 2014-03-11 at 11.52.53 PM.png"'], u'Content-Disposition': [u'attachment; filename="Screen Shot 2014-03-11 at 11.52.53 PM.png"'], u'X-Attachment-Id': [u'f_hso95l860']}, u'body': {u'attachmentId': u'', u'size': 266378}, u'partId': u'1', u'filename': u'Screen Shot 2014-03-11 at 11.52.53 PM.png'}

The GET response of Users.messages.attachments is:

{ "data": "", "size": 194659 }

When I decoded the data in Python as follows:

decoded_data1 = base64.b64decode(resp["data"])
decoded_data2 = email.utils._bdecode(resp["data"]) # email, the standard module
with open("image1.png", "w") as f:
    f.write(decoded_data1)
with open("image2.png", "w") as f:
    f.write(decoded_data2)

Both the file image1.png and image2.png have size 188511 and they are invalid png files as I couldn't open them in image viewer. Am I not using the correct base64 decoding for MIME body?

3 Answers 3

7

hmm. The Gmail API looks a bit different from the standard Python email module, but I did find an example of how to download and store an attachment:

https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get#examples

example

for part in message['payload']['parts']:
      if part['filename']:

        file_data = base64.urlsafe_b64decode(part['body']['data']
                                             .encode('UTF-8'))

        path = ''.join([store_dir, part['filename']])

        f = open(path, 'w')
        f.write(file_data)
        f.close()
Sign up to request clarification or add additional context in comments.

2 Comments

That was it. I didn't notice there was a python example available. Thanks a lot!
That did it! I was trying to get the body, but the base64.urlsafe_b64decode was failing since I hadn't encoded the data to utf-8. Using base64.b64decode works with the data as is (no encoding), but was giving weird strings. I didn't quite get it since apparently data == data.encode('utf-8') is True, but it makes sense now :)
2

You need to use urlsafe base64 decoding. so base64.urlsafe_b64decode() should do it.

Comments

2

I'd also like to note that you'll likely want to write binary. Such as:

f = open(path, 'w')

should be:

f = open(path, 'wb')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.