Reputation: 307
I create token using http://localhost:8080/auth/realms/{realm_name}/protocol/openid-connect/token endpoint
.
grant_type=client_credentials
client-id: ------------
client-secret: 78296d38-cc82-4010-a817-65c283484e51
Now I want to get users of realm. Then I send request to http://localhost:8080/auth/admin/realms/{realm_name}/users?username=demo
endpoint with token.
But I got 403 forbidden
response with "error": "unknown_error"
. How to solve it?
Upvotes: 22
Views: 69185
Reputation: 1
As the response code (403
) says forbidden
, it means that server has understood the request but you don't have the permissions to request that API.
So to get the access to view the users/groups/roles which are available in the Keycloak you must have to map the roles to the user. You can follow the below path to map any roles.
CLick on Users --> select your user --> click on Role Mapping --> click on Assign Roles --> Filter by clients --> select the roles and save
Upvotes: 0
Reputation: 81
Login to your Keycloak admin console and navigate to the "Client Scopes" section.
Click the "Create" button to create a new client scope “openid“ as
default.
then go to your realm client select client scopes tab.
then add the openid scope.
Upvotes: 3
Reputation: 141
to create(add) user
send POST request to:
http://localhost:8180/admin/realms/YOUR_REALM_NAME/users
with this body sample:
{
"firstName":"Amir",
"lastName":"Sharafkar", "email":"[email protected]", "enabled":"true",
"username":"sharafkar",
"credentials":[{
"type":"password",
"value":"1234",
"temporary":false
}]}
to get all users
send GET request to:
http://localhost:8180/admin/realms/YOUR_REALM_NAME/users
with "Authorization" key header with value: Bearer {YOUR_TOKEN}
to get individual user
send GET request to:
http://localhost:8180/admin/realms/YOUR_REALM_NAME/users/{id}
with "Authorization" key header with value: Bearer {YOUR_TOKEN}
DO NOT FORGET - Keycloak "version: 20.0.2"
assign role to your client with this steps:
Upvotes: 11
Reputation: 61
You need to assign a target realm-management role for your custom user. E.g. Keycloak version 19.02 to assign any realm-management role such as manage-users, manage-clients or realm-admin, you must follow these steps:
Filter by client, realm-management roles
Upvotes: 6
Reputation: 31
I ran into the same issue with the quarkus-Version 18.0.2:
I found this: "client_id is a confidential client that belongs to the realm master" here: https://github.com/keycloak/keycloak-documentation/blob/main/server_development/topics/admin-rest-api.adoc
I don't know why this restriction was introduced, but when you fetch your token from master (/auth/realms/master/protocol/openid-connect/token), then you are allowed to use a custom client and everything is fine.
Upvotes: 1
Reputation: 1750
The service account associated with your client needs to be allowed to view the realm users.
Go to http://localhost:8080/auth/admin/{realm_name}/console/#/realms/{realm_name}/clients
Select your client (which must be a confidential client)
In the settings tab, switch Service Account Enabled to ON
Click on save, the Service Account Roles tab will appear
In Client Roles, select realm_management
Scroll through available roles until you can select view_users
Click on Add selected
You should have something like this :
You client is now allowed to access users through the REST API.
Upvotes: 53