Python - Parse gmail messages().get() with format='raw'

Deadly 提交于 2019-12-06 08:19:05

问题


I'm using gmail api to get messages from my mailbox. But when using format='raw' in get method, I can't parse the returned data in 'raw' filed.

service.users().messages().get(userId='me', id=message_id, format='raw').execute()

This is in the document,

"raw": Returns the full email message data with body content in the raw field as a base64url encoded string; the payload field is not used.

and I tried

  • base64.urlsafe_b64decode(raw.encode('utf-8'))
  • base64.urlsafe_b64decode(raw.encode('ascii'))
  • base64.urlsafe_b64decode(raw.encode('cp932')) the message is in Japanese
  • base64.urlsafe_b64decode(raw)

None of the returned value is readable.

I got all ASCII words and numbers, but none of the Japanese words are readable:

  • Delivered-To
  • Received
  • X-Received
  • ...
  • Content-Type: text/plain; charset="UTF-8"
  • Content-Transfer-Encoding: base64

And when it comes to Japanese (or message body?), the message turns to something like: '''6Ie855Sw5qeYDQoNCuOBhOOBpOOCguOBiuS4luipseOBq+OBquOBo+OBpuOBiuOCiuOBvuOBmeOA gg0K44Oq44Ol44Km44Gn44GZ ...... PjwvZGl2PjwvZGl2PjwvZGl2PjwvZGl2PjwvZGl2PjwvZGl2Pg0KPC9kaXY+ PC9kaXY+PC9kaXY+DQo='''

Please help me out!!

来源:https://stackoverflow.com/questions/46431189/python-parse-gmail-messages-get-with-format-raw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!