Keycloak problems with API and endpoints

202 views Asked by At

Keycloak last version 22.0.3 by docker downloaded from official site

I have problems with API and endpoints... So i'm trying to solve using both togheter^^

With API i can't login... So i did this

import requests
from keycloak import KeycloakAdmin

url = "http://0.0.0.0:8080/realms/master/protocol/openid-connect/token"

data = {
    'client_id': 'admin-cli',
    'grant_type': 'password',
    'username': 'admin',
    'password': 'admin',
    'client_secret': 'YTC8x190ZsrV0pQZAWRqXPw9pKlbzniQ'
}

response = requests.post(url, data=data, verify=False)
if response.status_code == 200:
    token_data = response.json()
    access_token = token_data['access_token']
    print('Access Token:', access_token)
else:
    print('Failed to retrieve the access token. Status code:', response.status_code)

And login is ok. After i try to use the token with API to retrieve userinfo

# Configura il client per comunicare con Keycloak
keycloak_admin = KeycloakAdmin(server_url="http://0.0.0.0:8080/",
                               username="admin",
                               password="admin",
                               realm_name="master",
                               client_id="admin-cli",
                               verify=False)

# Ottieni la lista degli utenti
users = keycloak_admin.get_users(access_token)

# Stampa la lista degli utenti
for user in users:
    print(f"User ID: {user['id']}")
    print(f"Username: {user['username']}")
    print(f"Email: {user.get('email', 'N/A')}")
    print("------")

# Chiudi la sessione
keycloak_admin.logout()

and get this error

Traceback (most recent call last):
  File "/home/mirko/flask-app/k2.py", line 32, in <module>
    users = keycloak_admin.get_users(access_token)
  File "/usr/local/lib/python3.10/dist-packages/keycloak/keycloak_admin.py", line 147, in get_users
    data_raw = self.connection.raw_get(URL_ADMIN_USERS.format(**params_path), **query)
TypeError: keycloak.connection.ConnectionManager.raw_get() argument after ** must be a mapping, not str
1

There are 1 answers

0
tascio On

Ok, sorry, that problem was because a new restart of the docker and reset configuration. The real error is this

Traceback (most recent call last):
  File "/home/mirko/flask-app/k2.py", line 24, in <module>
    keycloak_admin = KeycloakAdmin(server_url="http://0.0.0.0:8080/",
  File "/usr/local/lib/python3.10/dist-packages/keycloak/keycloak_admin.py", line 68, in __init__
    self._token = keycloak_openid.token(username, password)
  File "/usr/local/lib/python3.10/dist-packages/keycloak/keycloak_openid.py", line 193, in token
    return raise_error_from_response(data_raw, KeycloakGetError)
  File "/usr/local/lib/python3.10/dist-packages/keycloak/exceptions.py", line 100, in raise_error_from_response
    raise error(error_message=message,
keycloak.exceptions.KeycloakAuthenticationError: 401: b'{"error":"unauthorized_client","error_description":"Invalid client or Invalid client credentials"}'

admin-cli have permission checked Client Authentication ON Standard Flow Direct Access Grant

And the Admin user is email verified