Skip to content

Instantly share code, notes, and snippets.

@njgibbon
Created June 6, 2019 19:33
Show Gist options
  • Save njgibbon/383a164465380f38b1b9c9dca2f9d4a6 to your computer and use it in GitHub Desktop.
Save njgibbon/383a164465380f38b1b9c9dca2f9d4a6 to your computer and use it in GitHub Desktop.
Keycloak admin API example in python based on https://gist.github.com/paoloantinori/84c644efb44002493835
import requests
import json
keycloak_url = 'http://localhost:8080/'
admin_token_endpoint = 'auth/realms/master/protocol/openid-connect/token'
realms_endpoint = 'auth/admin/realms'
bearer_token = ''
url=''
print("get the admin bearer token")
data = dict (
username='admin',
password='admin',
grant_type='password',
client_id='admin-cli',
)
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
url=keycloak_url+admin_token_endpoint
response = requests.post(url, data=data, headers=headers)
print(response)
print("get realms list")
response = response.json()
bearer_token = 'Bearer ' + response.get('access_token')
headers = {
'Accept': 'application/json',
'Authorization': bearer_token
}
url=keycloak_url+realms_endpoint
response = requests.get(url, headers=headers)
print(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment