title: Getting an access token using OAuth, the response appears to be encoded
body: <p>I'm pretty new to OAuth and am trying to refresh a token using a simple java client. Everything seems to go ok (or I think it does), the problem is I can't tell from the response if it's a good one. The http response code is 200, but when trying to parse the response for access_token, I get a null. Also difficult to troubleshoot is the &quot;raw&quot; response is garbled, or encoded in some way. I was thinking maybe it's byte but it doesn't seem to be. Here's the code:</p>
  <pre><code>    private static String getClientCredentials() {  
          String postParams = &quot;grant_type=refresh_token&amp;refresh_token=1234567890&quot;;
          Pattern pat = Pattern.compile(&quot;.*\&quot;access_token\&quot;\\s*:\\s*\&quot;([^\&quot;]+)\&quot;.*&quot;);
          String clientId = &quot;myClientID123&quot;;
          String clientSecret = &quot;myClientSecret123&quot;;
          String tokenUrl = &quot;https://www.host.com/oauth2/tenant/token&quot;;
          String auth = clientId + &quot;:&quot; + clientSecret;
          String authentication = Base64.getEncoder().encodeToString(auth.getBytes());
          
          BufferedReader reader = null;
          HttpsURLConnection connection = null;
          String returnValue = &quot;&quot;;
          try {
              URL url = new URL(tokenUrl);
              connection = (HttpsURLConnection) url.openConnection();
              connection.setRequestMethod(&quot;POST&quot;);
              connection.setRequestProperty(&quot;Authorization&quot;, &quot;Basic &quot; + authentication);
              connection.setRequestProperty(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
              connection.setRequestProperty(&quot;Accept&quot;, &quot;application/x-www-form-urlencoded&quot;);
              connection.setRequestProperty(&quot;Accept-Encoding&quot;, &quot;gzip, deflate, br&quot;);
              connection.setRequestProperty(&quot;Connection&quot;, &quot;keep-alive&quot;);
              connection.setDoOutput(true);
              
              OutputStream outStream = connection.getOutputStream();
              outStream.write(postParams.getBytes());
              outStream.flush();
              outStream.close();
                          
              System.out.println(&quot;Resp code: &quot; + connection.getResponseCode());
              System.out.println(&quot;Resp message: &quot; + connection.getResponseMessage());
  
              reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
              String line = null;
              StringWriter out = new StringWriter(connection.getContentLength() &gt; 0 ? connection.getContentLength() : 2048);
              
              while ((line = reader.readLine()) != null) {
                  out.append(line);
              }
              String response = out.toString();
              
              Matcher matcher = pat.matcher(response);
              if (matcher.matches() &amp;&amp; matcher.groupCount() &gt; 0) {
                  returnValue = matcher.group(1);
              }
  
              System.out.println(&quot;response: &quot; + response);
              System.out.println(&quot;returnValue: &quot; + returnValue);
              
          } catch (Exception e) {
              System.out.println(&quot;Error: &quot; + e.getMessage());
          } finally {
              if (reader != null) {
                  try {
                      reader.close();
                  } catch (IOException e) {
                  }
              }
              connection.disconnect();
          }
          return returnValue;
      }
  </code></pre>
  <p>Here's the console output, sorry, I had to screenshot it because the characters didn't paste right:</p>
  <p><a href="https://i.stack.imgur.com/j1NoG.jpg" rel="nofollow noreferrer">Response Screenshot</a></p>
  <p>Am I trying something that isn't allowed or is there a way to decode the response so I can run the pattern match to extract only the access_token? Or am I going about it all wrong? Any help is greatly appreciated in advance!</p>