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

3

8

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.

Howler answered 23/1, 2018 at 4:45 Comment(2)
by using that keycloak object i'm able to get the list of users.Howler
but i want to know how we get the list of user and session detalis by using http-post or rest api of keycloak.Howler
V
8

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.

Vibraculum answered 28/1, 2020 at 13:47 Comment(3)
Updated url auth/admin/realms/<realm>/usersNonna
Is the total records count value still returned? I cannot find it on v20.0.2Doubletime
The response structure did not work when I opened the docs. Looks like they have moved the Total count to a separate endpoint entirely: "Returns the number of users that match the given criteria."Vibraculum
C
4

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.

Claresta answered 19/4, 2018 at 7:27 Comment(5)
I don't think this will work since if you look at keycloak rest admin api which is keycloak.org/docs-api/5.0/rest-api/index.html then under the GET /{realm}/users endpoint details there are only first and max query parameters available. Did you verify this query working or not?Gant
Check github.com/keycloak/keycloak-community/blob/master/design/…Elapse
Hi, @Subodh do you know how to get many users by a list of ids?Boise
@Boise I think Ids are secure ,but you can check the official rest-api documentation .Claresta
I did, but it seems there is no way to get it from admin api, just try my luck in SO.Boise
F
1

According with official documentation :)

POST /{realm}/users 
mean
POST {{keycloak_url}}/admin/realms/{{keycloak_realm}}/users

Ex. POST https://mykeycloak/admin/realms/myrealm/users
Filthy answered 12/1, 2023 at 16:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.