<div dir="ltr">Hi Jason,<div><br></div><div>do you really need to use the raw REST API or would it be an option to use the keycloak-admin-client API?</div><div><br></div><div>here is an example for creating and deleting a user via the admin-client API:</div><div><br></div><div><div>package de.tdlabs.training.keycloak;</div><div><br></div><div>import static java.util.Arrays.asList;</div><div><br></div><div>import javax.ws.rs.core.Response;</div><div><br></div><div>import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;</div><div>import org.keycloak.admin.client.Keycloak;</div><div>import org.keycloak.admin.client.KeycloakBuilder;</div><div>import org.keycloak.representations.idm.CredentialRepresentation;</div><div>import org.keycloak.representations.idm.UserRepresentation;</div><div><br></div><div>public class KeycloakAdminClientExample {</div><div><br></div><div><span class="" style="white-space:pre">        </span>public static void main(String[] args) throws Exception {</div><div><br></div><div><span class="" style="white-space:pre">                </span>Keycloak kc = KeycloakBuilder.builder() //</div><div><span class="" style="white-space:pre">                                </span>.serverUrl(&quot;<a href="http://localhost:8081/auth">http://localhost:8081/auth</a>&quot;) //</div><div><span class="" style="white-space:pre">                                </span>.realm(&quot;rest-example&quot;)//</div><div><span class="" style="white-space:pre">                                </span>.username(&quot;rest-user-admin&quot;) //</div><div><span class="" style="white-space:pre">                                </span>.password(&quot;password&quot;) //</div><div><span class="" style="white-space:pre">                                </span>.clientId(&quot;admin-cli&quot;) //</div><div><span class="" style="white-space:pre">                                </span>.resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()) //</div><div><span class="" style="white-space:pre">                                </span>.build();</div><div><br></div><div><span class="" style="white-space:pre">                </span>CredentialRepresentation credential = new CredentialRepresentation();</div><div><span class="" style="white-space:pre">                </span>credential.setType(CredentialRepresentation.PASSWORD);</div><div><span class="" style="white-space:pre">                </span>credential.setValue(&quot;test123&quot;);</div><div><span class="" style="white-space:pre">                </span>credential.setTemporary(false);</div><div><br></div><div><span class="" style="white-space:pre">                </span>UserRepresentation user = new UserRepresentation();</div><div><span class="" style="white-space:pre">                </span>user.setUsername(&quot;testuser&quot;);</div><div><span class="" style="white-space:pre">                </span>user.setFirstName(&quot;Test&quot;);</div><div><span class="" style="white-space:pre">                </span>user.setLastName(&quot;User&quot;);</div><div><span class="" style="white-space:pre">                </span>user.setCredentials(asList(credential));</div><div><span class="" style="white-space:pre">                </span>user.setEnabled(true);</div><div><span class="" style="white-space:pre">                </span>user.setRealmRoles(asList(&quot;admin&quot;));</div><div><br></div><div><span class="" style="white-space:pre">                </span>// Create testuser</div><div><span class="" style="white-space:pre">                </span>Response result = kc.realm(&quot;rest-example&quot;).users().create(user);</div><div><span class="" style="white-space:pre">                </span>if (result.getStatus() != 201) {</div><div><span class="" style="white-space:pre">                        </span>System.err.println(&quot;Couldn&#39;t create user.&quot;);</div><div><span class="" style="white-space:pre">                        </span>System.exit(0);</div><div><span class="" style="white-space:pre">                </span>}</div><div><span class="" style="white-space:pre">                </span>System.out.println(&quot;Testuser created.... verify in keycloak!&quot;);</div><div><br></div><div><span class="" style="white-space:pre">                </span>System.out.println(&quot;Press any key...&quot;);</div><div><span class="" style="white-space:pre">                </span>System.in.read();</div><div><br></div><div><span class="" style="white-space:pre">                </span>// Delete testuser</div><div><span class="" style="white-space:pre">                </span>String locationHeader = result.getHeaderString(&quot;Location&quot;);</div><div><span class="" style="white-space:pre">                </span>String userId = locationHeader.replaceAll(&quot;.*/(.*)$&quot;, &quot;$1&quot;);</div><div><span class="" style="white-space:pre">                </span>kc.realm(&quot;rest-example&quot;).users().get(userId).remove();</div><div><span class="" style="white-space:pre">        </span>}</div><div>}<br></div></div><div><br></div><div><br></div><div><a href="https://gist.github.com/thomasdarimont/43689aefb37540624e35">https://gist.github.com/thomasdarimont/43689aefb37540624e35</a><br></div><div><br></div><div>Cheers,</div><div>Thomas</div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-01-12 19:05 GMT+01:00 JasonPeng <span dir="ltr">&lt;<a href="mailto:hpeng@redhat.com" target="_blank">hpeng@redhat.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;font-size:14px;font-family:Calibri,sans-serif;color:rgb(0,0,0)"><div>Hi there,</div><div><br></div><div>Can someone give me some hint about how to correctly setup a client that can accept REST request from a httpclient in keycloak?</div><div>For example, I use the admin-access example from keycloak source project and modify it to do a DELETE action through REST API, however I keep getting the request Forbidden 403 from keycloak server. I don’t change any setting of the admin-client imported from the json file and my code snippet of deleting user as below:</div><div><br></div><div><div>public static void deleteUser(HttpServletRequest request, AccessTokenResponse res) throws Failure {</div><div><span style="white-space:pre-wrap">        </span>HttpClient client = new DefaultHttpClient();</div><div><span style="white-space:pre-wrap">        </span>String userId = &quot;e20277f8-2ebe-4e5e-aa00-0cee9c578249&quot;;</div><div><span style="white-space:pre-wrap">        </span>try {</div><div><span style="white-space:pre-wrap">                </span>HttpDelete delete = new HttpDelete(getBaseUrl(request) + &quot;/admin/realms/demo/users/&quot; + userId);</div><div><span style="white-space:pre-wrap">                </span>delete.addHeader(&quot;Authorization&quot;, &quot;Bearer &quot; + res.getToken());</div><div><span style="white-space:pre-wrap">                </span>HttpResponse response = client.execute(delete);</div><div><span style="white-space:pre-wrap">                </span>System.out.println(response.getStatusLine().getReasonPhrase());</div><div><span style="white-space:pre-wrap">                </span>if (response.getStatusLine().getStatusCode() != 200) {</div><div><span style="white-space:pre-wrap">                        </span>throw new Failure(response.getStatusLine().getStatusCode());</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">                </span>HttpEntity entity = response.getEntity();</div><div><span style="white-space:pre-wrap">                </span>InputStream is = entity.getContent();</div><div><span style="white-space:pre-wrap">                </span>if (is != null)</div><div><span style="white-space:pre-wrap">                        </span>is.close();</div><div><span style="white-space:pre-wrap">        </span>} catch (IOException e) {</div><div><span style="white-space:pre-wrap">                </span>throw new RuntimeException(e);</div><div><span style="white-space:pre-wrap">        </span>} finally {</div><div><span style="white-space:pre-wrap">                </span>client.getConnectionManager().shutdown();</div><div><span style="white-space:pre-wrap">        </span>}</div><div>}</div></div></div>
<br>_______________________________________________<br>
keycloak-user mailing list<br>
<a href="mailto:keycloak-user@lists.jboss.org">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>