Gmail api python编码/解码错误
我有个问题。我正在尝试使用gmail api阅读来自gmail的电子邮件。我按照这里的指示做了 为了在python 3上运行它,我做了一些更改,因此最终得到了以下代码:Gmail api python编码/解码错误,python,api,encoding,gmail,quoted-printable,Python,Api,Encoding,Gmail,Quoted Printable,我有个问题。我正在尝试使用gmail api阅读来自gmail的电子邮件。我按照这里的指示做了 为了在python 3上运行它,我做了一些更改,因此最终得到了以下代码: def GetMimeMessage(service, user_id, msg_id): try: message = service.users().messages().get(userId=user_id, id=msg_id, for
def GetMimeMessage(service, user_id, msg_id):
try:
message = service.users().messages().get(userId=user_id, id=msg_id,
format='raw').execute()
print ('Message snippet: %s' % message['snippet'])
msg_str = base64.urlsafe_b64decode(message['raw'].encode('utf8'))
mime_msg = email.message_from_bytes(msg_str)
print(mime_msg)
return mime_msg
except errors.HttpError as error:
print ('An error occurred: %s' % error)
现在输出非常接近我想要的,但是有一个问题,输出中匈牙利口音的字符很奇怪:
G=C3=A1bor
而不是Gábor
而且html标记也有点不完整:
Follow us: =09=09=09=09=09=09=09=09=09<a href=3D"http=
问题是,我似乎找不到正确解码的方法。非常感谢您的帮助。我自己经过几个小时的努力,终于找到了解决办法(至少对我的情况如此)。即使是通过线程是旧的,我把它张贴在这里,以便其他人可以找到它以后。下面是:
# Get your message
message = service.users().messages().get(userId=user_id, id=msg_id, format='full').execute()
# Get the right body data item (this part was my Heureka-moment as there are several 'data' keys)
content = message['payload']['parts'][0]['parts'][1]['body']['data']
# Encode
msg_body = base64.urlsafe_b64decode(content).decode('utf-8')
这个序列使反编码对我有效。对于base64和Google Gmail API的解码/编码问题,有很多不同的解决方案建议,但似乎每种情况都是独一无二的。希望此解决方案也能帮助您 谢谢Tuppitappi。用于Python的Gmail API文档可能会更清晰一些。以上的解决方案对我也适用。我可以通过以下方式检索内容:
msgs = message['payload']['parts']
msg = msgs[1]['body']['data']
MessagePart可能由不同的部分组成:
"parts": [
{
"partId": "0",
"mimeType": "text/plain",
"filename": "",
"headers": [
{ "name": "Content-type", "value": "text/plain;charset=utf-8" },
{ "name": "Content-Transfer-Encoding", "value": "quoted-printable" }
],
"body": {
"size": 1887,
"data": "ascii string to be decoded"
}
},
{
"partId": "1",
"mimeType": "text/html",
"filename": "",
"headers": [
{ "name": "Content-type", "value": "text/html;charset=utf-8" },
{ "name": "Content-Transfer-Encoding", "value": "quoted-printable" }
],
"body": {
"size": 66970,
"data": "longer ascii string to be decoded"
}
}
]
针对完整有效负载从中提取:
Resource representations An email message.
{ "id": string, "threadId": string, "labelIds": [
string ], "snippet": string, "historyId": unsigned long, "internalDate": long, "payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
] }, "sizeEstimate": integer, "raw": bytes }
我花了一整天的时间试图解决这个问题。谢谢!谷歌应该在他们的文档中涵盖这一点。
Resource representations An email message.
{ "id": string, "threadId": string, "labelIds": [
string ], "snippet": string, "historyId": unsigned long, "internalDate": long, "payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
] }, "sizeEstimate": integer, "raw": bytes }