Skip to content
Advertisement

How can I get other users info(username, firstname) by id? [Keycloak]

How can I get user keycloak attributes (username, firstname, email…) based on user id? The user I’m using in the Keycloak session has already the role view-users assigned so I should be able to list at least all users, is there any Keycloak class that I can use?

What I’m trying to achieve here is to avoid to replicate the keycloak users database to another local database, but doesn’t seem possible to access any other user info, besides the one in the current session…

Advertisement

Answer

You can use the Admin REST API. The detailed description of the relevant API is available here. Also you can use the JAVA wrapper API. Please find couple of examples below.

Example 1, REST:

Get an access token:

JavaScript

Get all users:

JavaScript

Sample output:

JavaScript

Get a user based by user id:

JavaScript

Example 2, JAVA API:

Get a user based on user ID:

JavaScript

The relevant dependencies for the example above are (please note that the versions might not be up-to-date):

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement