I'm trying to connect to a Keycloak server using the python-keycloak package in Python, but I'm encountering an error.
I'm using the docker image of quay.io/keycloak/keycloak
Here's the code snippet I'm using:
keycloak_openid = KeycloakOpenID(server_url="http://172.17.0.1:8080",
client_id=settings.client_id,
realm_name=settings.customers,
client_secret_key=settings.client_secret)
keycloak.exceptions.KeycloakConnectionError: Can't connect to server (HTTPConnectionPool(host='172.17.0.1', port=8080): Max retries exceeded with url: /realms/zinc-customers/protocol/openid-connect/token/introspect (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ff8fd33fd90>: Failed to establish a new connection: [Errno 111] Connection refused')))
Update:
I was using docker-compose 1.27.0 because of that it was available not creating a default network for containers, they were not in the same network. Now I've updated my docker-compose v2.24.5. so my docker-compose creates a default network for my container
Now this issue I'm facing with other piece of code in python-keycloak
keycloak_connection = KeycloakOpenIDConnection(
server_url="http://172.17.0.1:8080",
username='admin',
password='admin',
realm_name="master",
user_realm_name="master",
client_id="admin-client",
client_secret_key="TC36pLkQpf9A3z2DTZzFv30DoT9mYKxQ",
verify=True
)
keycloak_admin = KeycloakAdmin(connection=keycloak_connection)
the code snippet I've shared at the top now is working fine But the latter code snippet throws me the same error that I've shared above.
I tested "localhost:8080" also works with your new test code.
Versions
Demo docker-compose.yml
Test code
Save as get-admin-token.py
Copy admin-client's secret
Result