[英]HttpClient post to obtain a Bearer token from keycloak
I have set up a keycloak 11.0.2 in standalone mode.我已经在独立模式下设置了 keycloak 11.0.2。 It is up and running fine.
它运行良好。 I am able to create a POST-Request with Postman to obtain a bearer token.
我能够使用 Postman 创建一个 POST 请求来获取不记名令牌。 Now I want to obtain a token from the keycloak server with the Apache HttpClient.
现在我想使用 Apache HttpClient 从 keycloak 服务器获取令牌。 I don't know how to do it.
我不知道该怎么做。
This is my code, but it returns 400 error, and i had 415 as well:这是我的代码,但它返回 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();
This is the response:这是回应:
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]}}
I wonder how to do it right.我想知道如何做对。 I try to do it like Postman does it, but I am missing something
我尝试像 Postman 那样做,但我遗漏了一些东西
UPDATE / Edit: Now I am a step further, but i still can not understand the behaviour.更新/编辑:现在我更进一步了,但我仍然无法理解这种行为。 Here is my Code:
这是我的代码:
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());
Now i am getting as response:现在我得到回应:
result: {"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
But this is what i did sent?但这是我发送的? What am I doing wrong?
我究竟做错了什么?
Can you try with this BasicNameValuePair instead of sending as a json:您可以尝试使用此 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"));
I had the same problem.我有同样的问题。 I had something like this:
我有这样的事情:
HttpPost post = new HttpPost("https://localhost:8080/auth");
post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
StringBuilder json = new StringBuilder();
json.append("{");
json.append("\"grant_type\":\"client_credentials\",");
json.append("\"client_id\":\"hhle4Xas\",");
json.append("\"client_secret\":\"FUe9\",");
json.append("\"account_id\":\"1234567\"");
json.append("}");
I changed for this and it worked for me:我为此进行了更改,它对我有用:
HttpPost post = new HttpPost("https://localhost:8080/auth");
post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
StringBuilder json = new StringBuilder();
json.append("");
json.append("grant_type=client_credentials&");
json.append("client_id=hhle7x22kj4upzblgnr1zkdu&");
json.append("client_secret=SUe9O0LuUvarwqi7mj3p7EDe&");
json.append("account_id=7295536");
json.append("");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.