Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Facebook signed_request using Java returns malformed JSON

I'm trying to parse Facebook signed_request inside Java Servlet's doPost. And I decode the signed request using commons-codec-1.3's Base64. Here is the code which I used to do it inside servlet's doPost

String signedRequest = (String) req.getParameter("signed_request");
String payload = signedRequest.split("[.]", 2)[1];
payload = payload.replace("-", "+").replace("_", "/").trim();
String jsonString = new String(Base64.decodeBase64(payload.getBytes()));

when I System.out the jsonString it's malformed. Sometime's it misses the ending } of JSON sometime it misses "} in the end of the string.

How can I get the proper JSON response from Facebook?

like image 728
amadamala Avatar asked Sep 13 '11 17:09

amadamala


1 Answers

facebook is using Base64 for URLs and you are probably trying to decode the text using the standard Base64 algorithm. among other things, the URL variant doesn't required padding with "=".

  1. you could add the required characters in code (padding, etc)
  2. you can use commons-codec 1.5 ( new Base64(true)), where they added support for this encoding.
like image 57
yourfriend Avatar answered Sep 30 '22 19:09

yourfriend