Thanks for that snippet. It worked.
You are querying for the access_token and then using that to make API
calls, whats the difference between this access_token and the one I
generate from Realm settings under the 'Initial Access Token' tab? When I
use that access token, it gives me a 401 error.
On Tue, Jan 19, 2016 at 9:23 PM, Thomas Darimont <
thomas.darimont(a)googlemail.com> wrote:
Hello,
I could successfully retrieve a list of users with this:
import requests
# avoid proxies for localhost....
import os
os.environ['no_proxy'] = '127.0.0.1,localhost'
serverUrl = 'http://localhost:8080/auth'
realm = 'test-realm'
username = 'realm-admin'
password = 'xxxxxx'
clientId = 'realm-management'
clientSecret = 'yyyyyyyyyy'
# Fetch access token
payload = {'grant_type': ['password'], 'username': [username],
'password':
[password]}
response = requests.post('%s/realms/%s/protocol/openid-connect/token' %
(serverUrl, realm)
, auth=(clientId, clientSecret)
, data=payload)
response
access_token = response.json()['access_token']
# Lookup users by username pattern
headers = {'Authorization': 'Bearer %s' % (access_token),
'Content-Type':
'application/json'}
usernamePattern = 'test'
userSearchResponse =
requests.get('%s/admin/realms/%s/users?username=%s&first=0&max=10' %
(serverUrl, realm, usernamePattern)
, headers=headers)
userSearchResponse
userSearchResponse.text
Cheers,
Thomas
2016-01-19 15:02 GMT+01:00 Kunal K <kunal(a)plivo.com>:
> Hi all,
>
> I referred to the doc[0] regarding Initial Access Token and generated a
> new token for my realm.
>
> I'm using the python requests library to invoke the GET realms API[1] but
> I get a 401 Unauthorized status code. Am I doing it wrong? Below is my code.
>
> import requests
> authorization = {'Authorization': 'Bearer
eyJhbGciOiJS.<redacted>'}
>
> response =
requests.get('http://keycloak-server.com/auth/admin/realms';,
headers=authorization)
> response.status_code // 401
>
>
>
>
> [0]
>
https://keycloak.github.io/docs/userguide/keycloak-server/html/client-reg...
> [1]
>
http://keycloak.github.io/docs/rest-api/index.html#_get_accessible_realms
>
> --
> *KUNAL KERKAR *| PRODUCT ENGINEER
> Plivo, Inc. 340 Pine St, San Francisco - 94104, USA
> Web:
www.plivo.com | Twitter: @plivo <
http://twitter.com/plivo>, @tsudot
> <
http://twitter.com/tsudot>
>
> Free Incoming SMS for All US Short Codes – Get One Today!
> <
https://www.plivo.com/sms-short-code/?utm=emailsig>
>
> _______________________________________________
> keycloak-user mailing list
> keycloak-user(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/keycloak-user
>
--
*KUNAL KERKAR *| PRODUCT ENGINEER
Plivo, Inc. 340 Pine St, San Francisco - 94104, USA
Web:
www.plivo.com | Twitter: @plivo <
http://twitter.com/plivo>, @tsudot
<
http://twitter.com/tsudot>
Free Incoming SMS for All US Short Codes – Get One Today!
<
https://www.plivo.com/sms-short-code/?utm=emailsig>