Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 decoding of MIME email not working (GMail API)

I'm using the GMail API to retrieve an email contents. I am getting the following base64 encoded data for the body: http://hastebin.com/ovucoranam.md

But when I run it through a base64 decoder, it either returns an empty string (error) or something that resembles the HTML data but with a bunch of weird characters.

Help?

like image 613
Andy Hin Avatar asked Jul 17 '14 19:07

Andy Hin


2 Answers

I'm not sure if you've solved it yet, but GmailGuy is correct. You need to convert the body to the Base64 RFC 4648 standard. The jist is you'll need to replace - with + and _ with /.

I've taken your original input and did the replacement: http://hastebin.com/ukanavudaz

And used base64decode.org to decode it, and it was fine.

like image 149
urbanspr1nter Avatar answered Oct 20 '22 20:10

urbanspr1nter


You need to use URL (aka "web") safe base64 decoding alphabet (see rfc 4648), which it doesn't appear you're doing. Using the standard base64 alphabet may work sometimes but not always (2 of the characters are different).

Docs don't seem to consistently mention this important detail. Here's one where it does though: https://developers.google.com/gmail/api/guides/drafts

Also, if your particular library doesn't support the "URL safe" alphabet then you can do string substitution on the string first ("-" with "+" and "_" with "/") and then do normal base64 decoding on it.

like image 43
Eric D Avatar answered Oct 20 '22 19:10

Eric D