使用非 mime message.get 方法,我拉出正文/数据并尝试对其进行解码,以便简单地以英文显示消息。我遇到的问题是某些消息可以正常解码,然后突然出现错误的填充问题。我尝试替换 + 和 / 无济于事。
为什么会发生这种情况,我该如何解决?
这是我的 messages.get 方法:
try:
message = service.users().messages().get(userId=user_id, id=msg_id).execute()
# Pull Raw Message Body from Response
message_raw = message['payload']['parts'][0]['body']['data']
# Decode the raw message
message_decoded = base64.b64decode(message_raw)
# Print the messages
print message_decoded
更新
服务
gmail_service = build('gmail', 'v1', http=http)
回溯错误
Traceback (most recent call last):
File "gmail.py", line 166, in <module>
GetMessage(gmail_service, 'me', message_id)
File "gmail.py", line 155, in GetMessage
message_decoded = base64.b64decode(message_raw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 76, in b64decode
raise TypeError(msg)
TypeError: Incorrect padding
使用 urlsafe 时出现回溯错误
Traceback (most recent call last):
File "gmail.py", line 166, in <module>
GetMessage(gmail_service, 'me', message_id)
File "gmail.py", line 155, in GetMessage
message_decoded = base64.urlsafe_b64decode(message_raw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 112, in urlsafe_b64decode
return b64decode(s, '-_')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 71, in b64decode
s = _translate(s, {altchars[0]: '+', altchars[1]: '/'})
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 36, in _translate
return s.translate(''.join(translation))
TypeError: character mapping must return integer, None or unicode