<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 &#39;Initial Access Token&#39; 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">&lt;<a href="mailto:thomas.darimont@googlemail.com" target="_blank">thomas.darimont@googlemail.com</a>&gt;</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[&#39;no_proxy&#39;] = &#39;127.0.0.1,localhost&#39;</div><div><br></div><div>serverUrl = &#39;<a href="http://localhost:8080/auth" target="_blank">http://localhost:8080/auth</a>&#39;</div><div>realm = &#39;test-realm&#39;</div><div>username = &#39;realm-admin&#39;</div><div>password = &#39;xxxxxx&#39;</div><div>clientId = &#39;realm-management&#39;</div><div>clientSecret = &#39;yyyyyyyyyy&#39;</div><div><br></div><div># Fetch access token</div><div>payload = {&#39;grant_type&#39;: [&#39;password&#39;], &#39;username&#39;: [username], &#39;password&#39;: [password]}</div><div>response = requests.post(&#39;%s/realms/%s/protocol/openid-connect/token&#39; % (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()[&#39;access_token&#39;]</div><div><br></div><div># Lookup users by username pattern</div><div>headers = {&#39;Authorization&#39;: &#39;Bearer %s&#39; % (access_token), &#39;Content-Type&#39;: &#39;application/json&#39;}</div><div>usernamePattern = &#39;test&#39;<br></div><div>userSearchResponse = requests.get(&#39;%s/admin/realms/%s/users?username=%s&amp;first=0&amp;max=10&#39; % (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">&lt;<a href="mailto:kunal@plivo.com" target="_blank">kunal@plivo.com</a>&gt;</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&#39;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 = {&#39;Authorization&#39;: &#39;Bearer eyJhbGciOiJS.&lt;redacted&gt;&#39;}

response = requests.get(&#39;<a href="http://keycloak-server.com/auth/admin/realms" target="_blank">http://keycloak-server.com/auth/admin/realms</a>&#39;, 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>