Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmail API - body of the message contains dash and can't be base64 decoded

I have response from Gmail API encoded in base64.

I used com.google.api.client.util.Base64 to decode this response, and it worked fine.

Now I'm refactoring code and want to update versions of dependencies. I noticed that Base64 class is now deprecated and it's suggested to use com.google.common.io.BaseEncoding. However, when I do the following:

Message asdf = service.users().messages().get(gmailUser, message.getId()).execute();
List<MessagePartHeader> headers = asdf.getPayload().getHeaders();
String data = asdf.getPayload().getBody().getData();
String body = new String(BaseEncoding.base64().decode(data));

I get an error:

com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalArgumentException: com.google.common.io.BaseEncoding$DecodingException: Unrecognized character: -

And indeed, I have some dashes in data that I'm receiving.

Is this a bug in Gmail API? Is there a workaround for that?

like image 340
zorglub76 Avatar asked Sep 19 '25 10:09

zorglub76


1 Answers

Got it!

I shouldn't use BaseEncoding.base64().decode(data), but BaseEncoding.base64Url().decode(data).

like image 175
zorglub76 Avatar answered Sep 21 '25 00:09

zorglub76