Search code examples
javajsongoogle-app-engineplayframeworkjanrain

Java Play framework parsing JSON error


I want to setup Janrain authentication to my Play! project which is hosted on GAE and uses GAE module. But I get the following error while I try to login:

RuntimeException occured : Cannot parse JSON (check logs)

And Play highlighs the following line as error:

JsonElement rpxJson = rpxRequest.get().getJson();

Here is method that I use for token callback:

public static void tokenCallback(String token) {
    Properties p = Play.configuration;
    // Try the driver
    String rpxApi = p.getProperty("login.rpx.apiKey");


    WSRequest rpxRequest = WS.url("http://rpxnow.com/api/v2/auth_info");
    // get RPX
    rpxRequest.setParameter("token", token);
    rpxRequest.setParameter("apiKey", rpxApi);

    JsonElement rpxJson = rpxRequest.get().getJson();
    JsonElement profile = rpxJson.getAsJsonObject().get("profile");
    String identifier = profile.getAsJsonObject().getAsJsonPrimitive("identifier").getAsString();

    welcome(identifier);

}

And here is the error that I get from terminal:

Internal Server Error (500) for request POST /login/tokencallback

Execution exception (In /app/controllers/Login.java around line 27)
RuntimeException occured : Cannot parse JSON (check logs)

play.exceptions.JavaExecutionException: Cannot parse JSON (check logs)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.RuntimeException: Cannot parse JSON (check logs)
    at play.libs.WS$HttpResponse.getJson(WS.java:668)
    at controllers.Login.tokenCallback(Login.java:27)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
    ... 1 more
Caused by: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected EOF at line 2 column 1
    at com.google.gson.JsonParser.parse(JsonParser.java:65)
    at com.google.gson.JsonParser.parse(JsonParser.java:45)
    at play.libs.WS$HttpResponse.getJson(WS.java:665)
    ... 7 more
Caused by: com.google.gson.stream.MalformedJsonException: Expected EOF at line 2 column 1
    at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1310)
    at com.google.gson.stream.JsonReader.peek(JsonReader.java:390)
    at com.google.gson.JsonParser.parse(JsonParser.java:60)
    ... 9 more

What can I do? Please, help me to solve this problem.

Thanks in advance.


Solution

  • OK, Here is my first suggestion. Try using the HTTPS connection for the URL. I ran into some problems with the HTTP connection. Here is how I do the Janrain connection:

            WSRequest rpxRequest = WS.url("https://rpxnow.com/api/v2/auth_info");
            // get RPX
            rpxRequest.setParameter("token", token);
            rpxRequest.setParameter("apiKey", rpxApi);
    
            HttpResponse res = null;
            try {
                res = rpxRequest.post();
            } catch (JavaExecutionException ex) {
                Log.error("unknown error ", ex);
                Validation.addError("", "Unknown Error: please try again");
                Validation.keep();
                Secure.login();
            } catch (Exception ex) {
                Log.error("Most likely SSL error", ex);
                Validation.addError("", "SSL Error: please try again");
                Validation.keep();
                Secure.login();
            }
            if (res.getStatus() != 200) {
                Log.error("status 200 error");
                Validation.addError("", "Status 200 error: please try again");
                Validation.keep();
                Secure.login();
            }
            JsonElement rpxJson = res.getJson();
            JsonElement profile = rpxJson.getAsJsonObject().get("profile");
            JsonObject profileJson = profile.getAsJsonObject();