[keycloak-user] Invoking admin REST API.

Thomas Darimont thomas.darimont at googlemail.com
Tue Jan 19 10:53:35 EST 2016


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 at 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-registration.html#d4e1450
> [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 at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/keycloak-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/keycloak-user/attachments/20160119/02bf357a/attachment-0001.html 


More information about the keycloak-user mailing list