<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("<a href="http://localhost:8081/auth">http://localhost:8081/auth</a>") //</div><div><span class="" style="white-space:pre">                                </span>.realm("rest-example")//</div><div><span class="" style="white-space:pre">                                </span>.username("rest-user-admin") //</div><div><span class="" style="white-space:pre">                                </span>.password("password") //</div><div><span class="" style="white-space:pre">                                </span>.clientId("admin-cli") //</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("test123");</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("testuser");</div><div><span class="" style="white-space:pre">                </span>user.setFirstName("Test");</div><div><span class="" style="white-space:pre">                </span>user.setLastName("User");</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("admin"));</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("rest-example").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("Couldn't create user.");</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("Testuser created.... verify in keycloak!");</div><div><br></div><div><span class="" style="white-space:pre">                </span>System.out.println("Press any key...");</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("Location");</div><div><span class="" style="white-space:pre">                </span>String userId = locationHeader.replaceAll(".*/(.*)$", "$1");</div><div><span class="" style="white-space:pre">                </span>kc.realm("rest-example").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"><<a href="mailto:hpeng@redhat.com" target="_blank">hpeng@redhat.com</a>></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 = "e20277f8-2ebe-4e5e-aa00-0cee9c578249";</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) + "/admin/realms/demo/users/" + userId);</div><div><span style="white-space:pre-wrap">                </span>delete.addHeader("Authorization", "Bearer " + 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>