如何通过 keycloak admin rest api 通过 execute-actions-email 更新密码

How to update password via keyclaok admin rest api by execute-actions-email

我正在尝试在 keycloack 中触发密码重置过程,以便用户收到一封设置新密码的电子邮件。不幸的是,我总是收到

的 400 响应

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@89719e69; line: 1, column: 1]

我在描述的 api 上调用 keycloak:"PUT /admin/realms/{realm}/users/{id}/execute-actions-email" 具有以下对象:

{"actions":["UPDATE_PASSWORD"]}

见: http://www.keycloak.org/docs/rest-api/index.html#_send_a_update_account_email_to_the_user

解决方案:仅使用 ["UPDATE_PASSWORD"] 作为您请求的正文并且有效...

java中的一个:Entity.json("[\"UPDATE_PASSWORD\"]");

对于所有想要为 execute-actions-email 编写 REST 调用的人,这里是工作代码片段 -

CloseableHttpClient httpclient = HttpClients.createDefault();
AccessTokenResponse accessTokenResponse = authzClient.obtainAccessToken(adminUserName, adminPassword);
String urlResetPassword = keyCloakURL+"/admin/realms/"+realmName+"/users/"+userId+"/execute-actions-email";
HttpPut putRequest = new HttpPut(urlResetPassword);
accessTokenResponse = authzClient.obtainAccessToken(adminUserName, adminPassword);
putRequest.addHeader("Authorization", "bearer "+accessTokenResponse.getToken());
putRequest.addHeader("content-type", MediaType.APPLICATION_JSON);
putRequest.setHeader("Accept", MediaType.APPLICATION_JSON);
StringEntity jSonEntity = new StringEntity("[\"UPDATE_PASSWORD\"]");
putRequest.setEntity(jSonEntity);
CloseableHttpResponse response = httpclient.execute(putRequest);

谢谢。

正确的请求如下。

URL:http://{{HOSTNAME}}:{{PORT}}/auth/realms/{{REALM_NAME}}/users/{{USER_ID}}/execute-actions-email
HTTP-METHOD: PUT
BODY: ["UPDATE_PASSWORD"]
HEADERS: AUTHORIZATION BEARER {{TOKEN-GOES-HERE}}