python - What is the encoding of the body of Gmail message? How to decode it? -
i using python api gmail. querying messages , retrieving them correctly, body of messages looks total nonsense, when mime type it's said text/plain
or text/html
.
i have been searching on api docs, keep saying it's string, when must encoding... thought base64
encoding, trying decode python base64
gives me typeerror: incorrect padding
, either it's not base64
or i'm decoding badly.
i'd love provide example, since i'm handling sensitive information i'll have obfuscate bit...
{ "payload": { "mimetype": "multipart/mixed", "filename": "", "headers": [ ... ], "body": { "size": 0 }, "parts": [ { "mimetype": "multipart/alternative", "filename": "", "headers": [ { "name": "content-type", "value": "multipart/alternative; boundary=001a1140b160adc309053bd7ec57" } ], "body": { "size": 0 }, "parts": [ { "partid": "0.0", "mimetype": "text/plain", "filename": "", "headers": [ { "name": "content-type", "value": "text/plain; charset=utf-8" }, { "name": "content-transfer-encoding", "value": "quoted-printable" } ], "body": { "size": 4067, "data": "ls0tls0tls0tlsbgb3j3yxjkzwqgbwvzc2fnzsatls0tls0tls0tdqpgcm9toibmaw5rzwrjbia8am9ilwfwchnabglua2vkaw4uy29tpg0krgf0ztogu2f0lcbtzxagmywgmjaxnibhdca5ojmwiefndqptdwjqzwn0oibbchbsawnhdglvbibmb3igu2vuaw9yiejhy2tlbmqgrgv2zwxvcg..." }
the field i'm talking payload.parts[0].parts[0].body.data
. have truncated @ random point, doubt decodable that, point... encoding?
also, wouldn't hurt know in docs explicitly base64 (unless it's standard encoding mime?).
update: in end there bad luck involved. have 5 mails this, , turns out first 1 malformed, unknown reason. after moving on other ones, able decode of them suggested approaches in answers. thank all!
this base64.
your truncated message is:
---------- forwarded message ---------- from: linkedin <job-apps@linkedin.com> date: sat, sep 3, 2016 @ 9:30 subject: application senior backend develop
here's sample code:
i had remove last 3 characters truncated message because getting same padding error you. have garbage message you're trying decode.
import base64 body = "ls0tls0tls0tlsbgb3j3yxjkzwqgbwvzc2fnzsatls0tls0tls0tdqpgcm9toibmaw5rzwrjbia8am9ilwfwchnabglua2vkaw4uy29tpg0krgf0ztogu2f0lcbtzxagmywgmjaxnibhdca5ojmwiefndqptdwjqzwn0oibbchbsawnhdglvbibmb3igu2vuaw9yiejhy2tlbmqgrgv2zwxv" result = base64.b64decode(body) print(result)
update
here's snippet gettting , decoding message body. decoding part taken gmail api documentation:
message = service.users().messages().get(userid='me', id=msg_id, format='full').execute() msg_str = base64.urlsafe_b64decode(message['payload']['body']['data'].encode('utf8')) mime_msg = email.message_from_string(msg_str) print(msg_str)
reference doc: https://developers.google.com/gmail/api/v1/reference/users/messages/get#python
Comments
Post a Comment