gmail-api下载附件错误的base64编码

时间:2016-02-16 07:48:56

标签: gmail-api

我正在尝试使用gmail api下载电子邮件附件,当我在浏览器中看到原始电子邮件时,下面是显示的内容

Content-Type: application/pdf; name="Report.pdf"
Content-Disposition: attachment; filename="Report.pdf"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_ikot3mys0

JVBERi0xLjQKJf////8KMTMgMCBvYmoKPDwvTGVuZ3RoIDI0NzYKL1N1YnR5cGUgL1hNTAovVHlw
ZSAvTWV0YWRhdGEKPj4Kc3RyZWFtCjw/eHBhY2tldCBiZWdpbj0n77u/JyBpZD0nVzVNME1wQ2Vo
aUh6cmVTek5UY3prYzlkJz8+Cjx4OnhtcG1ldGEgeDp4bXB0az0iMy4xLTcwMSIgeG1sbnM6eD0i
YWRvYmU6bnM6bWV0YS8iPgogIDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcv
MTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+CiAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91
dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iPgogICAgICA8eG1w
OkNyZWF0ZURhdGU+MjAxNS0xMC0yN1QwNjozMTo1OFo8L3htcDpDcmVhdGVEYXRlPgogICAgICA8
eG1wOkNyZWF0b3JUb29sPk5pdHJvIFBybyA5ICAoOS4gMC4gNi4gMjApPC94bXA6Q3JlYXRvclRv
b2w+CiAgICAgIDx4bXA6TW9kaWZ5RGF0ZT4yMDE1LTEwLTI3VDA2OjMxOjU5WjwveG1wOk1vZGlm
eURhdGU+CiAgICAgIDx4bXA6TWV0YWRhdGFEYXRlPjIwMTUtMTAtMjdUMDY6MzE6NTlaPC94bXA6
TWV0YWRhdGFEYXRlPgogICAgPC9yZGY6RGVzY3JpcHRpb24+CiAgICA8cmRmOkRlc2NyaXB0aW9u
IHJkZjphYm91dD0iIiB4bWxuczpkYz0iaHR0cDovL3B1cmwub3JnL2RjL2VsZW1lbnRzLzEuMS8i
PgogICAgICA8ZGM6Zm9ybWF0PmFwcGxpY2F0aW9uL3BkZjwvZGM6Zm9ybWF0PgogICAgICA8ZGM6
Y3JlYXRvcj4KICAgICAgICA8cmRmOlNlcT4KICAgICAgICAgIDxyZGY6bGk+PC9yZGY6bGk+CiAg
ICAgICAgPC9yZGY6U2VxPgogICAgICA8L2RjOmNyZWF0b3I+CiAgICAgIDxkYzp0aXRsZT4KICAg
ICAgICA8cmRmOkFsdD4KICAgICAgICAgIDxyZGY6bGkgeG1sOmxhbmc9IngtZGVmYXVsdCI+PC9y

但是当我使用下面的gmail api时 卷曲https://www.googleapis.com/gmail/v1/users/sndata@celestica.com/messages/[mailid]/attachments/[attachmentid],它返回

{
 "size": 4596,
 "data": "JVBERi0xLjQKJf____8KMTMgMCBvYmoKPDwvTGVuZ3RoIDI0NzYKL1N1YnR5cGUgL1hNTAovVHlwZSAvTWV0YWRhdGEKPj4Kc3RyZWFtCjw_eHBhY2tldCBiZWdpbj0n77u_JyBpZD0nVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkJz8-Cjx4OnhtcG1ldGEgeDp4bXB0az0iMy4xLTcwMSIgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iPgogIDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI-CiAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iPgogICAgICA8eG1wOkNyZWF0ZURhdGU-MjAxNS0xMC0yN1QwNjozMTo1OFo8L3htcDpDcmVhdGVEYXRlPgogICAgICA8eG1wOkNyZWF0b3JUb29sPk5pdHJvIFBybyA5ICAoOS4gMC4gNi4gMjApPC94bXA6Q3JlYXRvclRvb2w-CiAgICAgIDx4bXA6TW9kaWZ5RGF0ZT4yMDE1LTEwLTI3VDA2OjMxOjU5WjwveG1wOk1vZGlmeURhdGU-CiAgICAgIDx4bXA6TWV0YWRhdGFEYXRlPjIwMTUtMTAtMjdUMDY6MzE6NTlaPC94bXA6TWV0YWRhdGFEYXRlPgogICAgPC9yZGY6RGVzY3JpcHRpb24-CiAgICA8cmRmOkRlc2NyaXB0 .....

}

正如您在第一行中看到的,某些符号“////”已转换为“____”,导致文件被解码为损坏的格式。

这是gmail api中的错误吗?

由于

文森特

2 个答案:

答案 0 :(得分:5)

我刚刚发现gmail api以base64url格式返回附件内容,其中标准Base64的'+'和'/'字符分别被' - '和'_'替换。

答案 1 :(得分:0)

#you have to determine the file name

f = open('file.png', 'wb')
f.write(file_data)
f.close()

现在您可以将file_data保存到文件中

StreamWriter