2

I've got trouble with creating users via restapi on my keycloak service again. It's creating a user but not assiging the correct role afterwards.

Figured out that the code failes on the following part

    private void assignRoleToUser(String userId, String role){
        Keycloak keycloak = getAdminKeycloak();
        UsersResource usersResource = keycloak.realm(realm).users();
        UserResource userResource = usersResource.get(userId);
        ClientResource clientResource =keycloak.realm(realm).clients().get(clientId);
        RoleRepresentation clientRole = clientResource.roles().get(role).toRepresentation(); //<-- here
        userResource.roles().clientLevel(clientId).add(Collections.singletonList(clientRole));
    }

It returns me a HTTP 404 error. Digging deeper and it looks like that the .roles() method failes. The User is created in my keycloak, just my role is not assigned. .roles():

@Path("/roles")
    RolesResource roles();

Any idea how to fix this? Getting frustrated by now.

DAme
  • 697
  • 8
  • 21
R. Polito
  • 544
  • 6
  • 21

1 Answers1

1

Look's like I got confused by the confusing api design of keycloak. Fixed it and put up a github gist:

https://gist.github.com/rosariop/0a689fe221760a3ce9cda77af0255f00

R. Polito
  • 544
  • 6
  • 21