Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

keycloak

I am trying to trigger the password-reset process in keycloack, such that the user receives an email to set a new password. Unfortunately I always get 400 response with

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]

I call keycloak on described api: "PUT /admin/realms/{realm}/users/{id}/execute-actions-email" with following object:

{"actions":["UPDATE_PASSWORD"]}

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

like image 663
user2451418 Avatar asked Feb 06 '17 15:02

user2451418


3 Answers

Solution: Use only ["UPDATE_PASSWORD"] as body for your request and it works...

an in java: Entity.json("[\"UPDATE_PASSWORD\"]");

like image 78
user2451418 Avatar answered Nov 12 '22 23:11

user2451418


For all those who want to write REST call for execute-actions-email, here is the working code snippet -

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);

Thanks.

like image 2
Nirmalya Avatar answered Nov 12 '22 23:11

Nirmalya


Correct Request will be as below.

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}}
like image 2
Alok Deshwal Avatar answered Nov 12 '22 21:11

Alok Deshwal