小编wya*_*ang的帖子

使用 Python 进行 base64 解码,并使用基于 REST 的 Gmail API 下载附件

我尝试使用新的 Gmail api 从特定的电子邮件消息部分下载图像附件。(https://developers.google.com/gmail/api/v1/reference/users/messages/attachments#resource)。

消息部分是:

{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'}

Users.messages.attachments 的 GET 响应是:

{“数据”:“”,“大小”:194659 }

当我在 Python 中解码数据时,如下所示:

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)
Run Code Online (Sandbox Code Playgroud)

文件 image1.png 和 …

python gmail gmail-api

2
推荐指数
1
解决办法
3116
查看次数

标签 统计

gmail ×1

gmail-api ×1

python ×1