Problem with connecting to Keycloak server using python-keycloak package

279 Views Asked by At

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.

1

There are 1 best solutions below

1
Bench Vue On

I tested "localhost:8080" also works with your new test code.

Versions

Docker Compose version v2.18.1
Python 3.11.4
Keycloak v23.0.3
python-keycloak 3.7.0
Windows 11 Pro (10.0.22621 Build 22621)

Demo docker-compose.yml

version: '3.7'

services:
  postgres:
    image: postgres
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: keycloak
      POSTGRES_USER: keycloak
      POSTGRES_PASSWORD: password

  keycloak:
    image: quay.io/keycloak/keycloak:latest  # Update to the latest Keycloak image
    command: start-dev
    environment:
      KC_DB: postgres
      KC_DB_URL: jdbc:postgresql://postgres/keycloak
      KC_DB_USERNAME: keycloak
      KC_DB_PASSWORD: password
      KC_HTTP_ENABLED: true  # Enable HTTP if you're not using HTTPS
      KC_HEALTH_ENABLED: true
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: admin
    ports:
      - 8080:8080
    restart: always
    depends_on:
      - postgres

volumes:
  postgres_data:
    driver: local

Test code

Save as get-admin-token.py

from keycloak import KeycloakOpenIDConnection, KeycloakAdmin

keycloak_connection = KeycloakOpenIDConnection(
                        server_url="http://localhost:8080",
                        username='admin',
                        password='admin',
                        realm_name="master",
                        user_realm_name="master",
                        client_id="admin-client",
                        client_secret_key="5RfpBC1IlrJ1RDsAFi50o7hWTy9cIf2C",
                        verify=True
)
keycloak_admin = KeycloakAdmin(connection=keycloak_connection)
print(keycloak_admin.token)

Copy admin-client's secret

enter image description here

Result enter image description here