1

I have this JSON ouptup from GMAIL API: Message Body Parts (get message gmail api)

text/plain

SGVsbG8gaHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tLyBhbmQgdGhhbmsgeW91IGZvciB5b3VyIGhlbHAhLg0K

text/html

PGRpdiBkaXI9Imx0ciI-PGRpdiBjbGFzcz0iZ21haWxfZGVmYXVsdCIgc3R5bGU9ImZvbnQtZmFtaWx5OnZlcmRhbmEsc2Fucy1zZXJpZiI-SGVsbG_CoDxhIGhyZWY9Imh0dHA6Ly9zdGFja292ZXJmbG93LmNvbS8iPmh0dHA6Ly9zdGFja292ZXJmbG93LmNvbS88L2E-IGFuZCB0aGFuayB5b3UgZm9yIHlvdXIgaGVscCEuPC9kaXY-PGRpdiBjbGFzcz0iZ21haWxfZGVmYXVsdCIgc3R5bGU9ImZvbnQtZmFtaWx5OnZlcmRhbmEsc2Fucy1zZXJpZiI-PGJyPjwvZGl2PjxkaXY-PGJyPjwvZGl2PjxkaXYgY2xhc3M9ImdtYWlsX3NpZ25hdHVyZSIgZGF0YS1zbWFydG1haWw9ImdtYWlsX3NpZ25hdHVyZSI-PGRpdiBkaXI9Imx0ciI-PGRpdj48ZGl2IGRpcj0ibHRyIj48ZGl2PjxkaXYgZGlyPSJsdHIiPjxkaXY-PGRpdiBkaXI9Imx0ciI-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-PC9kaXY-DQo8L2Rpdj4NCg==

I can't decode message, text/plain or text/html. I tried many ways, but didn't work.

//result can be text/plain or text/html string from above
import android.util.Base64;
String orig = result.replaceAll("-","+").replaceAll("_","/");
byte[] a = Base64.decode(orig, Base64.DEFAULT); // byte[] result have content
String emailResult = new String(a,"UTF-8");
Log.i("EMAIL ", emailResult); //blank result
//or - also tried 
import com.google.api.client.util.Base64;
String c = new String(Base64.decodeBase64(result));
Log.i("EMAIL ", a); // a is blank

result length must be multiple of 4? Do I have to delete characters and replace it with ==s ?

Daniel
  • 11
  • 1
  • 6

1 Answers1

0
import android.util.Base64;

int flags = Base64.NO_WRAP | Base64.URL_SAFE;
byte[] data = Base64.decode(result, flags);
String emailResult = new String(data, "UTF-8");

Log.i("EMAIL ", emailResult);

// => EMAIL <div dir="ltr"> <div class="gmail_default" ...
Tholle
  • 108,070
  • 19
  • 198
  • 189
  • @Daniel You are right. I updated the answer. Does that work? – Tholle Jul 26 '16 at 13:33
  • While code often speaks for itself, it's good to add some explanation to your code. This popped up in the review queue, as code-only answers tend to. – Will Jul 26 '16 at 21:10
  • SGVsbG8gaHR0cDovL3N0YWNrb3ZlcmZsb3cuY29tLyBhbmQgdGhhbmsgeW91IGZvciB5b3VyIGhlbHAhLg0K this works if decode if I delete the last two characters or add == at the end of the sting, Why? – Daniel Jul 28 '16 at 20:39
  • Have you tried [this](http://stackoverflow.com/questions/26353710/how-to-achieve-base64-url-safe-encoding-in-c)? – Tholle Jul 28 '16 at 20:42
  • I tried it without success . Still no documentation about gmail api message decode from base64url to UTF-8 what to change in the body message to convert to base64 and then convert to String, etc... – – Daniel Jul 29 '16 at 02:55
  • @Tholle here is the thing, is not about the decode process the issue is the result from JSON GMAIL API, if I delete the last 2, 3 or 4 character the decode works, now the question is why is that? and what is the logic to delete 2, 3 or 4 characters... – Daniel Jul 31 '16 at 01:19
  • @Daniel I have no idea. You don't have to remove the ending padding `=` in e.g. JavaScript, but there must be a solution out there. `decodeURIComponent(escape(atob(result.replace(/\-/g, '+').replace(/\_/g, '/'))))` works in JavaScript. – Tholle Jul 31 '16 at 11:14
  • Tholle thanks anyway!. @GMAILAPI any gmail api engineer who can explain this logic? – Daniel Jul 31 '16 at 17:54