title: HttpClient post to obtain a Bearer token from keycloak
body: <p>I have set up a keycloak 11.0.2 in standalone mode. It is up and running fine. I am able to create a POST-Request with Postman to obtain a bearer token. Now I want to obtain a token from the keycloak server with the Apache HttpClient. I don't know how to do it.</p>
  <p>This is my code, but it returns 400 error, and i had 415 as well:</p>
  <pre><code>            CloseableHttpClient client = HttpClients.createDefault();
              HttpPost httpPost = new HttpPost(&quot;http://localhost:8180/auth/realms/Demo-Realm/protocol/openid-connect/token&quot;);
              httpPost.addHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
              httpPost.addHeader(&quot;grant_type&quot;,&quot;password&quot;);
              httpPost.addHeader(&quot;client_secret&quot;,&quot;30e8ebdf-7fbb-449d-9d94-709166b879b0&quot;);
              httpPost.addHeader(&quot;client_id&quot;,&quot;springboot-microservice&quot;);
              httpPost.addHeader(&quot;username&quot;,&quot;employee1&quot;);
              httpPost.addHeader(&quot;password&quot;,&quot;mypassword&quot;);
              CloseableHttpResponse response = client.execute(httpPost);
              System.out.println(&quot;response: &quot; + response.toString());
              client.close();
  </code></pre>
  <p>This is the response:</p>
  <pre><code>response: HttpResponseProxy{HTTP/1.1 400 Bad Request [Cache-Control: no-store, X-XSS-Protection: 1; mode=block, Pragma: no-cache, X-Frame-Options: SAMEORIGIN, Referrer-Policy: no-referrer, Date: Tue, 17 Nov 2020 14:31:31 GMT, Connection: keep-alive, Strict-Transport-Security: max-age=31536000; includeSubDomains, X-Content-Type-Options: nosniff, Content-Type: application/json, Content-Length: 84] ResponseEntityProxy{[Content-Type: application/json,Content-Length: 84,Chunked: false]}}
  </code></pre>
  <p>I wonder how to do it right. I try to do it like Postman does it, but I am missing something</p>
  <p>UPDATE / Edit: Now I am a step further, but i still can not understand the behaviour. Here is my Code:</p>
  <pre><code>      String result = &quot;&quot;;
              HttpPost post = new HttpPost(&quot;http://localhost:8180/auth/realms/Demo-Realm/protocol/openid-connect/token&quot;);
              post.setHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded;charset=UTF-8&quot;);
              StringBuilder json = new StringBuilder();
              json.append(&quot;{&quot;);
              json.append(&quot;\&quot;grant_type\&quot;:\&quot;password\&quot;&quot;);
              json.append(&quot;\&quot;client_id\&quot;:\&quot;springboot-microservice\&quot;,&quot;);
              json.append(&quot;\&quot;username\&quot;:\&quot;employee1\&quot;&quot;);
              json.append(&quot;\&quot;password\&quot;:\&quot;mypassword\&quot;&quot;);
              json.append(&quot;}&quot;);
              post.setEntity(new StringEntity(json.toString()));
              try (CloseableHttpClient httpClient = HttpClients.createDefault();
                   CloseableHttpResponse response = httpClient.execute(post)) {
                  result = EntityUtils.toString(response.getEntity());
              }
              System.out.println(&quot;result: &quot; + result.toString());
  </code></pre>
  <p>Now i am getting as response:</p>
  <pre><code>result: {&quot;error&quot;:&quot;invalid_request&quot;,&quot;error_description&quot;:&quot;Missing form parameter: grant_type&quot;}
  </code></pre>
  <p>But this is what i did sent? What am I doing wrong?</p>