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

2019-04-11 23:47发布

问题:

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!!