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

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

Open
bmorse526 opened this issue May 9, 2024 · 5 comments
Open

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

bmorse526 opened this issue May 9, 2024 · 5 comments

Comments

@bmorse526
Copy link

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')
<end_code>

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
Copy link
Author

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
Copy link
Collaborator

ryshoooo commented May 9, 2024

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
Copy link
Author

bmorse526 commented May 9, 2024 via email

@bmorse526
Copy link
Author

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
Copy link
Author

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?

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