python-3.x API和端点的密钥伪装问题

tct7dpnv  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(138)

Keycloak最新版本22.0.3由docker从官方网站下载
我遇到了API和端点问题.所以我想用两个togheter^^
用API我无法登录.所以我做了这个

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)

Login是OK的。在我尝试使用带有API的令牌来检索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()

得到这个错误

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
wa7juj8i

wa7juj8i1#

好的,对不起,这个问题是因为重新启动了docker并重置了配置。真实的错误是这样的

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-channelhave permission checked客户端身份验证ON标准流直接访问授权
管理员用户已通过电子邮件验证

相关问题