marcospereirampj / python-keycloak

MIT License
692 stars 296 forks source link

cannot import name 'Keycloak' from 'keycloak' #559

Open bmorse526 opened 2 months ago

bmorse526 commented 2 months ago

Full error message: ImportError: cannot import name 'Keycloak' from 'keycloak' (C:\Program Files\Python312\Lib\site-packages\keycloak__init__.py) Python 3.12

keycloak-client 0.15.4 (currently) have tried keycloak 1.15 with same error

VS Code for IDE (Have tried Python native IDE with same error

I have uninstalled any version of keycloak and made sure there was only 1.

I have tried Keycloak and keycloak neither work. I have copied code snippets from multiple sites and other that variable names the content matches. from keycloak import keycloak kc = keycloak.Keycloak(server_url="https://xx.yy.com/kc/auth/", verify=False) response = kc.token(client_id='me', client_secret='shush')

I need to get a TOKEN from keycloak to authenticate to an API server. any help would be appreciated. It looks like many people are doing it, but I cannot get around the import issue.
bmorse526 commented 2 months ago

image

Capture of the Keycloak folder with the init.py file

Contents of file

-- coding: utf-8 --

#

Copyright (C) 2017 Marcos Pereira marcospereira.mpj@gmail.com

#

This program is free software: you can redistribute it and/or modify

it under the terms of the GNU Lesser General Public License as published by

the Free Software Foundation, either version 3 of the License, or

(at your option) any later version.

#

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU Lesser General Public License for more details.

#

You should have received a copy of the GNU Lesser General Public License

along with this program. If not, see http://www.gnu.org/licenses/.

from .keycloak_admin import from .keycloak_openid import

ryshoooo commented 2 months ago

You're referencing code here from different packages called keycloak-client and keycloak. This is a project for the package python-keycloak. If you wish to use python-keycloak, make sure only python-keycloak package is installed an d no other is used as keycloak-client or keycloak don't work well together with python-keycloak. Then I'd suggest to follow our getting started guide for OIDC client https://python-keycloak.readthedocs.io/en/latest/modules/openid_client.html

bmorse526 commented 2 months ago

Thank you. That layed out more than I have found.

I have removed all libraries for keycloak and only re-installed python-keycloak. Using the reference you provided I have this code.

from keycloak import KeycloakOpenID

Configure client

keycloak_openid = KeycloakOpenID(server_url="https://xx.yy.com/kc/", client_id="client_id", realm_name="realm", client_secret_key="shush", verify=False)

token = keycloak_openid.token("user", "userPw")

userinfo = keycloak_openid.userinfo(token['access_token'])

I am getting this error Traceback (most recent call last): File "c:\filepath\API\KeyCloak try 3.py", line 26, in token = keycloak_openid.token("user_id", "userPw") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\site-packages\keycloak\keycloak_openid.py", line 319, in token return raise_error_from_response(data_raw, KeycloakPostError) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\site-packages\keycloak\exceptions.py", line 192, in raise_error_from_response raise error( keycloak.exceptions.KeycloakPostError: 400: b'{"error":"invalid_client","error_description":"Parameter client_assertion_type is missing"}'

I am not sure where to go from there. I have a client and a user set up in keycloak as it appears the token code needs a user id and PW. I would prefer using the client entry with its secret key to get a token, but have not found a way in the documentation to do it. I appreciate any help or direction you could provide. I am after getting a bearer token for use in an API header call (Authorization: Bearer asdfjkaskhfskdfksahf )

On Thu, May 9, 2024 at 1:12 PM Richard Nemeth @.***> wrote:

You're referencing code here from different packages called keycloak-client and keycloak. This is a project for the package python-keycloak. If you wish to use python-keycloak, make sure only python-keycloak package is installed an d no other is used as keycloak-client or keycloak don't work well together with python-keycloak. Then I'd suggest to follow our getting started guide for OIDC client https://python-keycloak.readthedocs.io/en/latest/modules/openid_client.html

— Reply to this email directly, view it on GitHub https://github.com/marcospereirampj/python-keycloak/issues/559#issuecomment-2103344937, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADTA4VTVC6CQ7PSO5YW4E63ZBPKB7AVCNFSM6AAAAABHPNE3TGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMBTGM2DIOJTG4 . You are receiving this because you authored the thread.Message ID: @.***>

bmorse526 commented 2 months ago

Sorry had a typo in the code. (user name) after correcting it I get

File "c:\Users\bob.morse\Documents\Python\API\KeyCloak try 3.py", line 26, in token = keycloak_openid.token("userID", "UserPw") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\site-packages\keycloak\keycloak_openid.py", line 319, in token return raise_error_from_response(data_raw, KeycloakPostError) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\site-packages\keycloak\exceptions.py", line 192, in raise_error_from_response raise error( keycloak.exceptions.KeycloakPostError: 400: b'{"error":"invalid_client","error_description":"Parameter client_assertion_type is missing"}'

Sorry for the bad previous post

bmorse526 commented 2 months ago

The end state I need is a Authorization: Bearer token. Once the token is returned is that the token object or is it a property of the token object?