Skip to content

Instantly share code, notes, and snippets.

@paoloantinori
Created January 26, 2016 15:59
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 19 You must be signed in to fork a gist
  • Save paoloantinori/84c644efb44002493835 to your computer and use it in GitHub Desktop.
Save paoloantinori/84c644efb44002493835 to your computer and use it in GitHub Desktop.
Keycloak Admin API Rest Example
#!/bin/bash
export TKN=$(curl -X POST 'http://localhost:8080/auth/realms/master/protocol/openid-connect/token' \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin" \
-d 'password=admin' \
-d 'grant_type=password' \
-d 'client_id=admin-cli' | jq -r '.access_token')
curl -X GET 'http://localhost:8080/auth/admin/realms' \
-H "Accept: application/json" \
-H "Authorization: Bearer $TKN" | jq .
@phuongtailtranminh
Copy link

OSX - Use Homebrew to install jq 1.5 with brew install jq.

@johnreytanquinco
Copy link

Hi, I am trying to run this script but getting the response below.

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   156  100    80  100    76     57     54  0:00:01  0:00:01 --:--:--    57
parse error: Invalid numeric literal at line 1, column 54
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     6  100     6    0     0      5      0  0:00:01  0:00:01 --:--:--     5
parse error: Invalid numeric literal at EOF at line 1, column 6

I assume this has to do with the proper use of quotes?

@avoidik
Copy link

avoidik commented Feb 28, 2018

@johnreytanquinco I guess you have to check encoding of the script-file, it should be LF, not CRLF

@Allan-Nava
Copy link

Login with python - request ?

@amitcvr1987
Copy link

How to use access_token obtained by above URL for requesting other Rest Admin API of Keycloak, like how to request to get all users in specific realm -
I am using "http://localhost:8280/auth/admin/realms/dev/users" and providing the Authorization header, but still it's responding with 403- Forbidden error.
Any answer ?

@mssaisandeep
Copy link

Any update on this? I am also struggling

@evasyuk
Copy link

evasyuk commented Nov 26, 2019

@mssaisandeep most probably you need to use correct service account with admin-role. Please, start with digging deeper into https://stackoverflow.com/questions/49511606/cannot-create-user-in-the-keycloak-getting-403-status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment