0

I was wondering if there's a way to manipulate the JSON return from the method postForObject of the class RestTemplate. I read on an old question about the class JSONObject but I noticed that has been replaced by JsonObject and I don't if it works exactly in the same way. Here's my method:

public void sendEmail() throws FirebaseAuthException {
    Body body = new Body(firebase.createCustomToken("username"), true);
    RestTemplate t = new RestTemplate();
    Object response = t.postForObject(
            "https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=[API_KEY]", 
            body, Object.class);
}

The firebase.createCustomToken("username") method is a method that I use to return a custom token for a user. The firebase object is an object of the class FireBaseService in which I wrote all the useful methods to work with FireBase. I passed as body of the POST method a String and a boolean. Everything works good but I don't know how to get the value of a certain JSON field. The value of response is:

{
  "idToken": "[ID_TOKEN]",
  "refreshToken": "[REFRESH_TOKEN]",
  "expiresIn": "3600"
}

The REST API call I'm doing is this: https://firebase.google.com/docs/reference/rest/auth#section-verify-custom-token.
How can I get the value of the field idToken?

YPD
  • 187
  • 13
  • 1
    Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – D. Lawrence Dec 09 '19 at 10:02

1 Answers1

0
JSONObject json = (JSONObject) response;
String token = json.getString("refreshToken");
jared
  • 473
  • 3
  • 16
  • `JSONObject` is deprecated in Java 11, which is the version i'm currently using. – YPD Dec 09 '19 at 10:11