Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting user keycloak Not Found exception

I can't get user groups like in samples. Samples from: Take a look at our testsuite. For example:

UserTest

GroupTest

Sample code from examples for receiving groups user is member of:

List<GroupRepresentation> membership = realm.users().get(user.getId()).groups();

My approach: 1. I create keycloak object for admin-cli client in myrealm realm:

this.keycloak = KeycloakBuilder.builder()
            .serverUrl("http://localhost:18080/auth")
            .realm("myrealm")
            .username("admin")
            .password("admin")
            .clientId("admin-cli")
            .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build())
            .build();
  1. When I try to get user:

    //this line works
    final UserResource userr = this.keycloak.realms().realm("myrealm").users().get("admin");
    
    //this two doesnt, in both result is javax.ws.rs.NotFoundException: HTTP 404 Not Found
    final UserRepresentation ur = userr.toRepresentation();
    final List<GroupRepresentation> groups = this.getRealm().users().get(user.getId()).groups();
    

In keycloak from admin-cli I created realm "myrealm" with 2 users and 2 groups Every user is member of both groups. admin is one of this users and is member of this two groups.

Users I've created are in "myrealm" realm, "admin" is one of them.

I've olso tried to give all available roles from clients and realm but this changes nothing.

admin-cli I meant keycloak app on localhost

What am I missing?

Libs I am using:

import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.UserResource;
import org.keycloak.admin.client.resource.UsersResource;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
like image 653
Adam Michalski Avatar asked Jan 11 '17 14:01

Adam Michalski


1 Answers

I just stumbled into the same problem as you. The problem was with the get method. Its argument is not a user name (admin), but a user identifier. Something like 494061c1-c8f3-44c9-8542-df895af81716 .

In my case I properly tried to pass the user ID, but I used token ID instead which is something completely different.

like image 118
David Hladky Avatar answered Oct 05 '22 03:10

David Hladky