Skip to content

Instantly share code, notes, and snippets.

@knikolla
Last active May 22, 2023 21:36
Show Gist options
  • Save knikolla/d81073476052b786b87c20e6c4e56ae7 to your computer and use it in GitHub Desktop.
Save knikolla/d81073476052b786b87c20e6c4e56ae7 to your computer and use it in GitHub Desktop.
Query all Keycloak Users from the HTTP API
import json
import logging
import requests
from requests.auth import HTTPBasicAuth
logger = logging.getLogger()
def get_client_token():
client_token = None
token_url = (
"<KEYCLOAK URL>/auth/realms/mss/protocol/openid-connect/token"
)
try:
r = requests.post(
token_url,
data={'grant_type': 'client_credentials'},
auth=HTTPBasicAuth(
'<PUT CLIENT ID HERE>',
'<PUT CLIENT SECRET HERE>'
)
)
token_response = r.json()
client_token = token_response.get('access_token', None)
except requests.exceptions.RequestException as re:
logger.error(
"An error occurred communicating with the server "
f"while trying to get the client_token. Error: {re}"
)
except requests.exceptions.JSONDecodeError as decode_error:
logger.debug(
"An error occurred decoding the client token "
f" returned by the server. Error: {decode_error}"
)
return client_token
session = requests.session()
headers = {
'Authorization': ("Bearer %s" % get_client_token()),
'Content-Type': 'application/json'
}
session.headers.update(headers)
users = []
for i in range(4):
first = i * 100
to_append = session.get(f'https://keycloak.mss.mghpcc.org/auth/admin/realms/mss/users?first={first}&max=100').json()
users += to_append
with open('keycloak_data.json', 'w') as f:
json.dump(users, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment