gpt4 book ai didi

java - HttpClient 发布以从 keycloak 获取 Bearer token

转载 作者:行者123 更新时间:2023-12-04 08:34:12 26 4
gpt4 key购买 nike

我已经在独立模式下设置了一个 keycloak 11.0.2。它运行良好。我可以使用 Postman 创建 POST 请求以获取不记名 token 。现在我想使用 Apache HttpClient 从 keycloak 服务器获取 token 。我不知道该怎么做。
这是我的代码,但它返回 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]}}
我想知道如何正确地做到这一点。我试着像 postman 那样做,但我错过了一些东西
更新/编辑:现在我更进一步,但我仍然无法理解这种行为。这是我的代码:
      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());
现在我得到的回应是:
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"));

关于java - HttpClient 发布以从 keycloak 获取 Bearer token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64877324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com