7

I need to configure realms in Keycloak by the Admin WS accessible at the path

http://KeycloakServer:8081/auth/admin/realms

So in Postman I run the following request

URL: http://KeycloakServer:8081/auth/admin/realms

Method: POST

Body:

{
    "enabled": true,
    "id": "TestRealm",
}

I get a 401 Unauthorized response, so I think it is necessary to authenticate to the Admin WS. But in the docs I cannot find any information about the type of authentication required and the syntax. Do you know how to authenticate to the Keycloak WSs?

Thank you

gvdm
  • 2,373
  • 4
  • 29
  • 59

2 Answers2

14

Do a POST request to https://hostname:8080/auth/realms/master/protocol/openid-connect/token

Sets the Content-Type to application/x-www-form-urlencoded

and put the following in the body:

username=<username>&password=<password>&client_id=admin-cli&grant_type=password
Andrea Scarpino
  • 731
  • 6
  • 13
  • In version 1.9.7 I am doing the exact same steps. I get the access_token which I then put in the header as follows Authorization: Bearer mytoken, but I still get 401 Unauthorized. Is it something I am missing? – Turbut Alin Jun 08 '16 at 18:32
  • I tried the solution it works, One possible reason why it didn't work for you is that the token expires in 60 seconds. So retry it. – johnwick0831 Feb 20 '18 at 02:23
2

This will do the trick for you. Do a post on

URI - /auth/realms/master/protocol/openid-connect/token

with Headers as below

  1. username=<admin username>
  2. password=<admin password>
  3. client_id=security-admin-console
  4. grant_type=password
  5. Content-Type=application/x-www-form-urlencoded

This will return you a JSON, with access_token in it. Take the token and use it as the Authorization Bearer Token in your request. It should work.

Let me know if you face any issue.

aksappy
  • 2,958
  • 3
  • 20
  • 44
  • 1
    Thank you for your reply @aksappy! However, it's incorrect because since 1.7.0 security-admin-console is disable by default and those fields are not headers but are part of the request body. I made a new answer. – Andrea Scarpino Feb 11 '16 at 16:52