how to get the list of users from keycloak by using httppost/rest-api

raana picture raana · Jan 23, 2018 · Viewed 10.2k times · Source

I am new to keycloak any help will be appreciable. I'm able to get the list of user details by using the keycloak api, I want to know how we can get it by using http-post.

Keycloak kc = Keycloak.getInstance("url-path", "realm", "username", "password", "admin-cli");

By using this keycloak object I am able to get the list of users. But when I want to get the list of users by using http-post or rest api call by using the end point url which is given by keycloak I am not able to get the access token based on token I am getting only the particular user , I want list of users by using rest api's.

Answer

Subodh Joshi picture Subodh Joshi · Apr 19, 2018

Below are the steps to achieve this by key-cloak rest-api first create the url

String fetchAllUsers = URL+"/admin/realms/"+realmName+"/users?";

Now if you have millions of users you don't want to do fetch all user in one go so keycloak support rest-api with pagination,in your rest post query you can ask tenantName,queryString,Limit,PageNumber

fetchAllUsers(String tenantName, List<String> queryString, String limit, String pageNumber) 

now these thing will help to create a query which will send only that many record which will not impact performance of keycloak as well as web-api.

In queryString user can pass the filter criteria like if he want to search by name,lastname,email-id or user want result in ascending or descending order.That you have to parse and create a query String so it will resemble to the rest query of keycloak. After doing that you can concatenate the query String with fetchAllUsers .

Your query will be like this

https://<IP ADDRESS>/<PORT>/admin/realms/<REALM NAME>/users?q=username=<USER NAME>ASC&email=<EMAIL ID>&limit=<RECORD LIMIT>&page=<PAGE NO>

Please ignore typo if you found anything.