Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python-Keycloak 4.3.0 Keycloak 25.0.2 NameError: name 'keycloak_openid' is not defined. Did you mean: 'KeycloakOpenID'? #589

Open
juparker37 opened this issue Aug 9, 2024 · 5 comments

Comments

@juparker37
Copy link

I am following the example from the docs page at https://python-keycloak.readthedocs.io/en/latest/modules/openid_client.html and https://python-keycloak.readthedocs.io/en/latest/reference/keycloak/keycloak_openid/index.html. I am not sure what other modules the API client is expecting?

I have imported the module as:

from keycloak import KeycloakOpenID

Both config_well_known = keycloak_openid.well_known() and oauth_request = keycloak_openid.auth_url() are failing with:

NameError: name 'keycloak_openid' is not defined. Did you mean: 'KeycloakOpenID'? [justin@cm01]$ python keycloak_client.py Traceback (most recent call last): File "keycloak_client.py", line 25, in <module> oauth_request = keycloak_openid.auth_url( ^^^^^^^^^^^^^^^

My pip3 list is:

`Package Version


anyio 4.4.0
asgiref 3.8.1
async-property 0.2.2
boto3 1.34.151
botocore 1.34.151
certifi 2024.7.4
cffi 1.16.0
charset-normalizer 3.3.2
contourpy 1.2.1
cryptography 43.0.0
cycler 0.12.1
deprecation 2.1.0
Django 5.1
django-cors-headers 4.4.0
django-filter 24.3
djangorestframework 3.15.2
fonttools 4.53.1
h11 0.14.0
httpcore 1.0.5
httpx 0.27.0
idna 3.7
jmespath 1.0.1
jwcrypto 1.5.6
kiwisolver 1.4.5
markup 0.2.2
matplotlib 3.9.1
networkx 3.3
numpy 2.0.1
packaging 24.1
pillow 10.4.0
pip 24.2
psycopg 3.2.1
psycopg-binary 3.2.1
pycparser 2.22
pyparsing 3.1.2
python-dateutil 2.9.0.post0
python-keycloak 4.3.0
requests 2.32.3
requests-toolbelt 1.0.0
s3transfer 0.10.2
setuptools 72.1.0
six 1.16.0
sniffio 1.3.1
sqlparse 0.5.1
typing_extensions 4.12.2
urllib3 2.2.2
wheel 0.43.0
xlwt 1.3.0
`

@Tarick
Copy link
Contributor

Tarick commented Aug 12, 2024

You missed the instruction step to run

# Configure client
# For versions older than 18 /auth/ must be added at the end of the server_url.
keycloak_openid = KeycloakOpenID(server_url="http://localhost:8080/",
                                 client_id="example_client",
                                 realm_name="example_realm",
                                 client_secret_key="secret")

@juparker37
Copy link
Author

juparker37 commented Aug 14, 2024

Yes, that block of code was already in place. To minimize confusion, here is the entire Python file.

`from django.shortcuts import redirect
from keycloak import KeycloakOpenID

keycloak_openid_connect = KeycloakOpenID(
server_url="https://server01.mydomain.com:9444/auth",
client_id="itadmin_client",
realm_name="ITADMIN",
client_secret_key="mysecretkey"
)

config_well_known = keycloak_openid.well_known()

oauth_auth_request = keycloak_openid.KeycloakOpenID.auth_url(
redirect_uri="https://server01.mydomain.com:9443/realms/ITADMIN/*",
scope="dj_admin@example.com",
state=""
)

oauth_access_token = keycloak_openid.token(
grant_type='authorization_code',
code='code_return_value',
redirect_uri="https://server01.mydoain.com:9443/realms/ITADMIN/*"
)
`

@Tarick
Copy link
Contributor

Tarick commented Aug 14, 2024

Change
keycloak_openid_connect = KeycloakOpenID(
to
keycloak_openid = KeycloakOpenID(

@juparker37
Copy link
Author

So I copied and pasted exacly what your example shows on the docs page and put in my variables, and both 'config_well_known' and 'oauth_auth_request' return module error.

`from keycloak import KeycloakOpenID

Configure client
For versions older than 18 /auth/ must be added at the end of the server_url.
keycloak_openid = KeycloakOpenID(server_url="https://server01.mydomain.com:9444/auth",
client_id="itadmin_client",
realm_name="ITADMIN",
client_secret_key="mysecretkey")

config_well_known = keycloak_openid.well_known()

auth_url = keycloak_openid.auth_url(
redirect_uri="https://server01.mydomain.com:9443/realms/ITADMIN/*",
scope="dj_admin@example.com",
state="")

access_token = keycloak_openid.token(
grant_type='authorization_code',
code='the_code_you_get_from_auth_url_callback',
redirect_uri="https://server01.mydoain.com:9443/realms/ITADMIN/*")
`

python keycloak_client.py Traceback (most recent call last): File "/home/user/cloudmonitor/authentication/keycloak_client.py", line 25, in <module> oauth_auth_request = keycloak_openid.KeycloakOpenID.auth_url( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'KeycloakOpenID' object has no attribute 'KeycloakOpenID'

python keycloak_client.py Traceback (most recent call last): File "/home/user/cloudmonitor/authentication/keycloak_client.py", line 10, in <module> config_well_known = keycloak_openid.well_known() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.local/lib/python3.11/site-packages/keycloak/keycloak_openid.py", line 252, in well_known return raise_error_from_response(data_raw, KeycloakGetError) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.local/lib/python3.11/site-packages/keycloak/exceptions.py", line 192, in raise_error_from_response raise error( keycloak.exceptions.KeycloakGetError: 404: b'<html><body><h1>Resource not found</h1></body></html>'

@juparker37
Copy link
Author

Issue seems to be resolved once added "import keycloak" in the Python script. The keycloak and python-keycloak site-packets module relationship is confusing.

I also use /auth in my URLS so I had to add the trailing / to the end of /auth. Now I am working thru 401 errors on the Keycloak side as I have not finished the Keycloak setup yet.

Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants