17

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?

Andy Hin
  • 30,345
  • 42
  • 99
  • 142

7 Answers7

30

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.

urbanspr1nter
  • 1,347
  • 2
  • 16
  • 25
16

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.

Eric D
  • 6,901
  • 1
  • 15
  • 26
  • I'm not sure. I am taking the encoded string directly from gmail's API (JSON response) and using the `NSData initWithBase64EncodedString:options:` method. It doesn't mention anything about a URL safe alphabet? – Andy Hin Jul 17 '14 at 20:02
  • 2
    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. hope that helps. – Eric D Aug 19 '14 at 16:45
7

I had the same issue decoding the 'data' fields in the message object response from the Gmail API. The Google Ruby API library wasn't decoding the text correctly either. I found I needed to do a url-safe base64 decode:

@data = Base64.urlsafe_decode64(JSON.parse(@result.data.to_json)["payload"]["body"]["data"])

Hope that helps!

TJ-
  • 14,085
  • 12
  • 59
  • 90
ben
  • 71
  • 1
2

There is an example for python 2.x and 3.x:

decodedContents = base64.urlsafe_b64decode(payload["body"]["data"].encode('ASCII'))
babca
  • 738
  • 7
  • 11
0

If you only need to decode for displaying purposes, consider using atob to decode the messages in JavaScript frontend (see ref).

FullStack
  • 5,902
  • 4
  • 43
  • 77
0

I found whilst playing with the API result, once I had drilled down to the body I was given an option to decode in the available methods.

  val message = mService!!.users().messages().get(user, id).setFormat("full").execute()

  println("Message snippet: " + message.snippet)
  if(message.payload.mimeType == "text/plain"){
        val body = message.payload.body.decodeData()  // getValue("body")

        Log.i("BODY", body.toString(Charset.defaultCharset()))
  }

The result:-
com.example.quickstart I/BODY: ISOLATE NORMAL: 514471,Fap, South Point Rolleston, 55 Faringdon Boulevard , Rolleston, 30 May 2018 20:59:21

Gary Robottom
  • 101
  • 1
  • 5
-1

I coped the base64 test to a file (b64.txt), then base64-decoded it using base64 (from coreutils) with the -d option (see http://linux.die.net/man/1/base64) and I got text that was perfectly readable. The command I used was:

cat b64.txt | base64 -d
mti2935
  • 11,465
  • 3
  • 29
  • 33
  • Hmmm weird! Trying to decode it using a service like: http://www.base64decode.org/ returns weird results. Also decoding it in objective-c using NSData::initWithBase64EncodedString returns nil (error). Any ideas what the difference could be? – Andy Hin Jul 17 '14 at 19:50
  • Possibly there may be some errors in the base64 encoding, and some decoders are more tolerant/forgiving, and others are not. Are you sure you copied the entire base64-encoded string correctly, and didn't miss any characters at the beginning or end of the string? – mti2935 Jul 17 '14 at 20:08
  • Andy, I'm not sure what you are doing wrong. I get perfectly normal-looking results using base64decode.org (with the default UTF-8 decoding). – mti2935 Jul 17 '14 at 20:44
  • scroll down a bit when you get the results and you will see a bunch of the characters are jumbled at the end. – Andy Hin Jul 17 '14 at 20:46