1

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?

LebroNan
  • 149
  • 7
  • 18

3 Answers3

2

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

ravthiru
  • 8,878
  • 2
  • 43
  • 52
  • Hi, I am user keycloak 3.1. I've tried to use kc.realm(realmName).users().delete(id), but it is still giving me a 401 unauthorized error. My guess is that there are something wrong with the initialization of that kc variable, wildfly doesn't recognize me as an admin user. But I don't know how to fix that. – LebroNan Aug 02 '17 at 13:49
  • Have you tried with "master" realm, You can get more information from keycloak server logs – ravthiru Aug 03 '17 at 01:43
  • I didn't try it but I don't think that's the problem. I am not using master realm for my application, I create this sensorcloud-auth realm for my application. – LebroNan Aug 03 '17 at 18:13
  • i believe user is assigned admin role – ravthiru Aug 04 '17 at 00:54
  • Yep, I think so. I can login in to admin console using username admin and password admin, then delete the user manually. But I just cannot do it programmatically. – LebroNan Aug 04 '17 at 14:46
0

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.

buzz3791
  • 1,683
  • 2
  • 20
  • 38
0

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