如何通过执行动作电子邮件通过keyclaok admin rest api更新密码

时间:2017-02-06 15:41:55

标签: json keycloak

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

  

com.fasterxml.jackson.databind.JsonMappingException:无法从START_OBJECT令牌中反序列化java.util.ArrayList的实例    在[来源: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

3 个答案:

答案 0 :(得分:5)

解决方案:仅使用["UPDATE_PASSWORD"]作为您的请求的正文,它可以正常运行...

在java中:Entity.json("[\"UPDATE_PASSWORD\"]");

答案 1 :(得分:1)

对于所有想要为执行动作电子邮件编写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);

感谢。

答案 2 :(得分:0)

正确的请求将如下所示。

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}}