You seem to be mixing things up a bit, I will provide a couple of examples on how you could use the keycloak admin client.
Personally I would choose example 2, creating a dedicated service account client as we are communicating service to service.
Example 1 -> Using a user
Create new client under your desired realm -> keycloak-admin
Select public client with only direct access grant enabled
Create new role, enable composite roles
type realm-managment into client roles under composite roles
add available roles that you need
Select a user and open role mappings tab
type keycloak-admin in client roles and add needed roles
@zonaut, thanks very much for your reply, it worked. I’m still pretty new to keycloak (OAuth and OIDC too for that matter), but I’m getting the hang of it.
I just found it weird to have so little information on the web about using the keycloak-admin-client. Maybe is not so common to use it?
Most probably don’t need more advanced workflows or don’t know the client exists at all.
And lot’s of others are just reading the API docs and use their favorite tools their used too.
I like this client though, versioned and stable.
One question please: I have tried Example # 2. It works for retrieving realms but not for creating new ones using the Rest API. It seems that this only works with the master realm. Is this correct?
I haven’t tried creating another realm from another realm yet. Only creating a new realm from the master realm which sounds the best way to do this in my opinion. The master realm is to manage all other realms which are or should be separated from each other.
But that’s my idea on how it should be, everybody is free to live on the edge of course
@zonaut
am follwoing the second example. create a new realm and client.
when am trying to create a new user , code throws below error.
javax.ws.rs.WebApplicationException: Create method returned status Forbidden (Code: 403); expected status: Created (201)
at org.keycloak.admin.client.CreatedResponseUtil.getCreatedId(CreatedResponseUtil.java:43)
at org.iftas.KeyCloakAdmin.main(KeyCloakAdmin.java:75