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.
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.
Keycloak comes with a fully functional Admin REST API with all features provided by the Admin Console. To invoke the API you need to obtain an access token with the appropriate permissions. The required permissions are described in Server Administration.
According to the Offical Keycloak Admin REST API Docs (Scroll down a bit to the Get users Returns a list of users, filtered according to query parameters section), you will find that there are 2 query parameters available: first
and max
.
If you set first
as 10 and max
as 15, then first 10 users will be skipped, and you will receive the next 15 i.e. users 11 to 25 in response.
Use the following request:
GET https://$KEYCLOAK_HOST/auth/admin/$REALM/users?first=10&max=15
P.S. - Don't forget the Authorization Header.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With