Java HttpClient post从KeyClope获取承载令牌

Java HttpClient post从KeyClope获取承载令牌,java,oauth-2.0,keycloak,apache-httpclient-4.x,bearer-token,Java,Oauth 2.0,Keycloak,Apache Httpclient 4.x,Bearer Token,我已经在独立模式下设置了KeyClope 11.0.2。它运转良好。我可以与邮递员创建一个POST请求,以获取不记名令牌。现在,我想通过ApacheHttpClient从Keyclope服务器获取一个令牌。我不知道怎么做 这是我的代码,但它返回400个错误,我还有415个错误: CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new Http

我已经在独立模式下设置了KeyClope 11.0.2。它运转良好。我可以与邮递员创建一个POST请求,以获取不记名令牌。现在,我想通过ApacheHttpClient从Keyclope服务器获取一个令牌。我不知道怎么做

这是我的代码,但它返回400个错误,我还有415个错误:

            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost("http://localhost:8180/auth/realms/Demo-Realm/protocol/openid-connect/token");
            httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
            httpPost.addHeader("grant_type","password");
            httpPost.addHeader("client_secret","30e8ebdf-7fbb-449d-9d94-709166b879b0");
            httpPost.addHeader("client_id","springboot-microservice");
            httpPost.addHeader("username","employee1");
            httpPost.addHeader("password","mypassword");
            CloseableHttpResponse response = client.execute(httpPost);
            System.out.println("response: " + response.toString());
            client.close();
以下是回应:

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]}}
result: {"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
我不知道如何把它做好。我试着像邮递员那样做,但我错过了一些东西

更新/编辑:现在我更进一步了,但我仍然无法理解这种行为。这是我的密码:

      String result = "";
            HttpPost post = new HttpPost("http://localhost:8180/auth/realms/Demo-Realm/protocol/openid-connect/token");
            post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            StringBuilder json = new StringBuilder();
            json.append("{");
            json.append("\"grant_type\":\"password\"");
            json.append("\"client_id\":\"springboot-microservice\",");
            json.append("\"username\":\"employee1\"");
            json.append("\"password\":\"mypassword\"");
            json.append("}");
            post.setEntity(new StringEntity(json.toString()));
            try (CloseableHttpClient httpClient = HttpClients.createDefault();
                 CloseableHttpResponse response = httpClient.execute(post)) {
                result = EntityUtils.toString(response.getEntity());
            }
            System.out.println("result: " + result.toString());
现在我得到的答复是:

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]}}
result: {"error":"invalid_request","error_description":"Missing form parameter: grant_type"}

但这是我送的吗?我做错了什么?

您是否可以尝试使用此BasicNameValuePair而不是作为json发送:

ArrayList<NameValuePair> parameters;
parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("grant_type", "password"));
parameters.add(new BasicNameValuePair("client_id", "springboot-microservice"));
parameters.add(new BasicNameValuePair("username", "employee1"));
parameters.add(new BasicNameValuePair("password", "mypassword"));
parameters.add(new BasicNameValuePair("client_secret", "30e8ebdf-7fbb-449d-9d94-709166b879b0"));
post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
ArrayList参数;
参数=新的ArrayList();
添加(新的BasicNameValuePair(“授权类型”、“密码”);
添加(新的BasicNameValuePair(“客户端id”,“springboot微服务”);
添加(新的BasicNameValuePair(“用户名”、“雇员1”);
添加(新的BasicNameValuePair(“密码”、“我的密码”);
添加(新的BasicNameValuePair(“客户机密”,“30e8ebdf-7fbb-449d-9d94-709166b879b0”);
post.setEntity(新的UrlEncodedFormEntity(参数,“UTF-8”);