1
votes

I am trying to delete a user from a realm using keycloak admin client api.

I am following an example from here: https://gist.github.com/thomasdarimont/43689aefb37540624e35

Here is what my code looks like:

Keycloak kc = KeycloakBuilder.builder().serverUrl("https://localhost:8445/auth")
        .realm("sensorcloud-auth").username("admin").password("admin").clientId("admin-cli")
        .clientSecret("b6b4f0ec-9936-46a2-9f40-69c207e2e0f2")
        .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()).build();
kc.realm("sensorcloud-auth").users().get("a3fdac49-f7eb-4be7-a81f-b48b09a6694c").remove();

I can login to keycloak admin console using username admin and password admin, and I am pretty sure there is no typo with other parameters, but every time when I try to delete the user with that userId, I will be given a 401 Unauthorized error. Can someone help me figure out what is going on?

3

3 Answers

2
votes

Not sure which version of keycloak admin client api you are using, with current API there is no remove operation.

It should be kc.realm(realmName).users().delete(id) . You can check the REST API for Delete User

0
votes

We kept getting HTTP 401 status code responses when the hostname stamped on the iss field of the bearer's/user's access token had a different case i.e. lowercase vs. uppercase than the url used to post an HTTP request to keycloak's token endpoint.

0
votes

While creating Keycloak instance, Realm should be master realm. After creating instance with Master Realm, you can delete the user by using your code.