2

Using the non-mime message.get method, I am pulling out the body/data and trying to decode it in order to simply show the message in english. The problem I am having is that some of the messages decode fine, and then suddenly I get incorrect padding issues. I have tried replacing + and / to no avail.

Why would this be happening and how do I fix it?

Here's my messages.get method:

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

UPDATES

Service

gmail_service = build('gmail', 'v1', http=http)

Traceback Error

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

Traceback error when using 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
sacshu
  • 397
  • 9
  • 20
  • What kind of error are you getting? Include the traceback. – msw Jul 13 '14 at 17:30
  • You sould also show an example of preblematic message. – Serge Ballesta Jul 13 '14 at 17:34
  • What module are you using; that is, what is the type of `service`? – msw Jul 13 '14 at 17:34
  • `+` and `/` are the 63rd and 64th characters in Base-64 (along with 52 letters and 10 digits). Padding refers to the presence or absence of `=` signs (0, 1 or 2 needed) at the end of a Base-64 string. Some systems are careless about adding the correct padding. – Jonathan Leffler Jul 13 '14 at 17:48
  • Thanks for the comments: @msw and Serge I've added the traceback. msw Not completely sure if that's what you wanted to see (see 'Service' in my updates'). Jonathan - thanks for the info, it does look like all the messages end in an '=', but perhaps there aren't enough... – sacshu Jul 13 '14 at 18:31
  • http://stackoverflow.com/questions/2941995/python-ignore-incorrect-padding-error-when-base64-decoding Seen that already? – matcheek Jul 13 '14 at 18:49
  • @SergeBallesta Added in traceback – sacshu Jul 13 '14 at 23:42

1 Answers1

11

You need to use:

message_decoded = base64.urlsafe_b64decode(message_raw)

since the raw message is URLSafe base64 encoded.

Jay Lee
  • 13,415
  • 3
  • 28
  • 59
  • 2
    When I use that this is the following error (Traceback in original post): TypeError: character mapping must return integer, None or unicode – sacshu Jul 13 '14 at 23:15
  • 6
    I think I figured it out - I needed to convert message_raw to a str before passing it into the urlsafe method. – sacshu Jul 14 '14 at 00:11