0

I want to get the individual parameters from the server response, for example I want to get the value of "access_token", so in this case: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxx", what can I do?

    void GetAccessToken(User user, String productID, String token) throws IOException
{
    CloseableHttpClient httpClient = null;
    HttpPost post = null;
    CloseableHttpResponse response = null;

    try
    {
        httpClient = HttpClientBuilder.create().build();
        post = new HttpPost("https://accounts.google.com/o/oauth2/token");
        ArrayList<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
        nvps.add(new BasicNameValuePair("grant_type", "refresh_token"));
        nvps.add(new BasicNameValuePair("client_id", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com"));
        nvps.add(new BasicNameValuePair("client_secret", "xxxxxxxxxxxxxxxxxxxx"));
        nvps.add(new BasicNameValuePair("redirect_uri", "https://www.my.app/"));
        nvps.add(new BasicNameValuePair("refresh_token", "1/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
        post.setEntity(new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
        response = httpClient.execute(post);
        int responseCode = response.getStatusLine().getStatusCode();
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println("response: " + response.toString());
        System.out.println("responseCode: " + responseCode);
        System.out.println("responseBody: " + responseBody);            
    }
    catch (Exception ex)
    {
        System.out.println("Error occured due to ::" + ex.getMessage());
    }
    finally
    {
        if (httpClient != null) httpClient.close();
        if (post != null) post.releaseConnection();
        if (response != null) response.close();

        Check_InApp_Purchase(user, productID, token);
    }
}

Server response complete:

GetAccessToken response: HttpResponseProxy{HTTP/1.1 200 OK [Cache-Control: 
no-cache, no-store, max-age=0, must-revalidate, Pragma: no-cache, Date: Thu, 04 Mar 2021 14:34:45 GMT, Expires: Mon, 01 Jan 1990 00:00:00 GMT, Content-Type: application/json; charset=utf-8, Vary: Origin, Vary: X-Origin, Vary: Referer, Server: scaffolding on HTTPServer2, X-XSS-Protection: 0, X-Frame-Options: SAMEORIGIN, X-Content-Type-Options: nosniff, Alt-Svc: h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43", Transfer-Encoding: chunked] org.apache.http.client.entity.DecompressingEntity@31612345}
responseCode: 200
responseBody: {
"access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxx",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/androidpublisher",
"token_type": "Bearer"
}

If the code has something wrong, don't hesitate to write it to me!

Ardito ITA
  • 19
  • 4
  • this question has been answered here: https://stackoverflow.com/a/20376055/2583044. you'll need to parse the response and get the "access_token" field value from the parsed object – 62mkv Mar 04 '21 at 13:21

1 Answers1

0

The solution I found is to split the content of "responseBody"

responseBody = responseBody.replaceAll("[ {}\"\n]+", "");
        String[] splitting = responseBody.split(",");
        String[] split = splitting[0].split(":");
        access_token = split[1];
Ardito ITA
  • 19
  • 4