<div dir="ltr">Thanks for that snippet. It worked.<div><br></div><div>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.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 19, 2016 at 9:23 PM, Thomas Darimont <span dir="ltr"><<a href="mailto:thomas.darimont@googlemail.com" target="_blank">thomas.darimont@googlemail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>I could successfully retrieve a list of users with this:</div><div><br></div><div><br></div><div><div>import requests</div><div><br></div><div># avoid proxies for localhost....</div><div>import os</div><div>os.environ['no_proxy'] = '127.0.0.1,localhost'</div><div><br></div><div>serverUrl = '<a href="http://localhost:8080/auth" target="_blank">http://localhost:8080/auth</a>'</div><div>realm = 'test-realm'</div><div>username = 'realm-admin'</div><div>password = 'xxxxxx'</div><div>clientId = 'realm-management'</div><div>clientSecret = 'yyyyyyyyyy'</div><div><br></div><div># Fetch access token</div><div>payload = {'grant_type': ['password'], 'username': [username], 'password': [password]}</div><div>response = requests.post('%s/realms/%s/protocol/openid-connect/token' % (serverUrl, realm)</div><div><span style="white-space:pre-wrap">        </span> , auth=(clientId, clientSecret)</div><div><span style="white-space:pre-wrap">        </span> , data=payload)</div><div>response</div><div>access_token = response.json()['access_token']</div><div><br></div><div># Lookup users by username pattern</div><div>headers = {'Authorization': 'Bearer %s' % (access_token), 'Content-Type': 'application/json'}</div><div>usernamePattern = 'test'<br></div><div>userSearchResponse = requests.get('%s/admin/realms/%s/users?username=%s&first=0&max=10' % (serverUrl, realm, usernamePattern)</div><div><span style="white-space:pre-wrap">        </span> , headers=headers)</div><div>userSearchResponse</div><div>userSearchResponse.text</div></div><div><br></div><div>Cheers,</div><div>Thomas</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">2016-01-19 15:02 GMT+01:00 Kunal K <span dir="ltr"><<a href="mailto:kunal@plivo.com" target="_blank">kunal@plivo.com</a>></span>:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr">Hi all,<div><br></div><div>I referred to the doc[0] regarding Initial Access Token and generated a new token for my realm.</div><div><br></div><div>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.</div><div><br></div><div></div><div><pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-radius:3px;display:inline;background-color:rgb(248,248,248);white-space:pre-wrap;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important">import requests
authorization = {'Authorization': 'Bearer eyJhbGciOiJS.<redacted>'}
response = requests.get('<a href="http://keycloak-server.com/auth/admin/realms" target="_blank">http://keycloak-server.com/auth/admin/realms</a>', headers=authorization)
response.status_code // 401
</code></pre><div title="MDH:PGRpdj5gYGA8L2Rpdj48ZGl2PmltcG9ydCByZXF1ZXN0czwvZGl2PjxkaXY+CgoKCgoKCgo8cCBj
bGFzcz0iIj48c3BhbiBjbGFzcz0iIj5hdXRob3JpemF0aW9uID0geydBdXRob3JpemF0aW9uJzog
J0JlYXJlciBleUpoYkdjaU9pSlNVekkxTmlKOS5leUpxZEdrLi4uJmx0O3JlZGFjdGVkJmd0Oyd9
PC9zcGFuPjwvcD48cCBjbGFzcz0iIj48c3BhbiBjbGFzcz0iIj4KCgoKCgoKCjwvc3Bhbj48L3A+
PHAgY2xhc3M9IiI+PHNwYW4gY2xhc3M9IiI+cmVzcG9uc2UgPSByZXF1ZXN0cy5nZXQoJ2h0dHA6
Ly9rZXljbG9hay1zZXJ2ZXIuY29tL2F1dGgvYWRtaW4vcmVhbG1zJywgaGVhZGVycz1hdXRob3Jp
emF0aW9uKTwvc3Bhbj48L3A+PHAgY2xhc3M9IiI+PHNwYW4gY2xhc3M9IiI+cmVzcG9uc2Uuc3Rh
dHVzX2NvZGUgLy8mbmJzcDs8L3NwYW4+NDAxPC9wPjxwIGNsYXNzPSIiPmBgYDwvcD48L2Rpdj4=" style="min-height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0"></div></div><div><p></p>
</div><div><br></div><div>[0] <a href="https://keycloak.github.io/docs/userguide/keycloak-server/html/client-registration.html#d4e1450" target="_blank">https://keycloak.github.io/docs/userguide/keycloak-server/html/client-registration.html#d4e1450</a><br clear="all"><div>[1] <a href="http://keycloak.github.io/docs/rest-api/index.html#_get_accessible_realms" target="_blank">http://keycloak.github.io/docs/rest-api/index.html#_get_accessible_realms</a></div><span><font color="#888888"><div><br></div>-- <br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><b style="font-size:small;font-family:tahoma,sans-serif">KUNAL KERKAR </b><font color="#666666" style="font-size:small;font-family:tahoma,sans-serif">| PRODUCT ENGINEER</font><br></div><div><div dir="ltr"><div style="font-size:small;font-family:arial"><div><span style="color:rgb(102,102,102);font-family:tahoma,sans-serif;font-size:13px;line-height:16.12px">Plivo, Inc. 340 Pine St, San Francisco - 94104, USA</span><font face="tahoma, sans-serif"><font color="#666666"><br></font></font></div><div><font face="tahoma, sans-serif"><font color="#666666">Web:</font> <a href="http://www.plivo.com/" style="color:rgb(17,85,204)" target="_blank">www.plivo.com</a> <font color="#666666">| Twitter:</font> <a href="http://twitter.com/plivo" style="color:rgb(17,85,204)" target="_blank">@plivo</a>, <a href="http://twitter.com/tsudot" style="color:rgb(17,85,204)" target="_blank">@tsudot</a></font></div><div><div dir="ltr" style="font-family:arial,sans-serif;font-size:12.8px"><div style="font-size:small;font-family:arial"><div><br></div><div><a href="https://www.plivo.com/sms-short-code/?utm=emailsig" style="color:rgb(17,85,204);font-family:tahoma,sans-serif" target="_blank">Free Incoming SMS for All US Short Codes – Get One Today!</a></div></div></div></div></div></div></div></div></div></div></div></div></div>
</font></span></div></div>
<br></div></div>_______________________________________________<br>
keycloak-user mailing list<br>
<a href="mailto:keycloak-user@lists.jboss.org" target="_blank">keycloak-user@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/keycloak-user" rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a><br></blockquote></div><br></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><b style="font-size:small;font-family:tahoma,sans-serif">KUNAL KERKAR </b><font color="#666666" style="font-size:small;font-family:tahoma,sans-serif">| PRODUCT ENGINEER</font><br></div><div><div dir="ltr"><div style="font-size:small;font-family:arial"><div><span style="color:rgb(102,102,102);font-family:tahoma,sans-serif;font-size:13px;line-height:16.1200008392334px">Plivo, Inc. 340 Pine St, San Francisco - 94104, USA</span><font face="tahoma, sans-serif"><font color="#666666"><br></font></font></div><div><font face="tahoma, sans-serif"><font color="#666666">Web:</font> <a href="http://www.plivo.com/" style="color:rgb(17,85,204)" target="_blank">www.plivo.com</a> <font color="#666666">| Twitter:</font> <a href="http://twitter.com/plivo" style="color:rgb(17,85,204)" target="_blank">@plivo</a>, <a href="http://twitter.com/tsudot" style="color:rgb(17,85,204)" target="_blank">@tsudot</a></font></div><div><div dir="ltr" style="font-family:arial,sans-serif;font-size:12.8000001907349px"><div style="font-size:small;font-family:arial"><div><br></div><div><a href="https://www.plivo.com/sms-short-code/?utm=emailsig" style="color:rgb(17,85,204);font-family:tahoma,sans-serif" target="_blank">Free Incoming SMS for All US Short Codes – Get One Today!</a></div></div></div></div></div></div></div></div></div></div></div></div></div>
</div>