<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; color: rgb(0, 0, 0); font-size: 14px; font-family: Calibri, sans-serif;"><div>Hi Thomas,</div><div><br></div><div>Thank’s for the recommendation. However, I bumped into some clossloader issue when I tried it on my EAP 6.4+ Keycloak 1.1.7.Final environment.</div><div><br></div><div>I keep get the class not found error against RestEasy Client, "java.lang.NoClassDefFoundError: org/jboss/resteasy/client/jaxrs/ResteasyClientBuilder”. Although I’d setup my jboss-deployment-structure.xml in my project under /WEB-INF/. The setting is like below:</div><div><br></div><div><div><jboss-deployment-structure></div><div> <deployment></div><div> <dependencies></div><div> <module name="org.jboss.resteasy.resteasy-jaxrs" services="import"/></div><div> </dependencies></div><div> </deployment></div><div></jboss-deployment-structure></div></div><div><br></div><div><br></div><div><div><div>Thank you and best regards,</div><div>— </div><div>Jason Peng</div><div>Solution Architect, Taiwan</div><div>Ret Hat Limited</div><div>TEL: +886-2-7743-2972</div><div>FAX: +886-2-7743-2974</div><div>Mobile: +886-988-836-827</div><div>EMAIL: hpeng@redhat.com</div><div><br></div><div><br></div></div></div><div><br></div><div><br></div><span id="OLK_SRC_BODY_SECTION"><div style="font-family:Calibri; font-size:11pt; text-align:left; color:black; BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0in; PADDING-LEFT: 0in; PADDING-RIGHT: 0in; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt"><span style="font-weight:bold">From: </span> Thomas Darimont <<a href="mailto:thomas.darimont@googlemail.com">thomas.darimont@googlemail.com</a>><br><span style="font-weight:bold">Date: </span> Wednesday, January 13, 2016 at 3:26 AM<br><span style="font-weight:bold">To: </span> JasonMacAir <<a href="mailto:hpeng@redhat.com">hpeng@redhat.com</a>><br><span style="font-weight:bold">Cc: </span> keycloak-user <<a href="mailto:keycloak-user@lists.jboss.org">keycloak-user@lists.jboss.org</a>><br><span style="font-weight:bold">Subject: </span> Re: [keycloak-user] How to correctly use REST API? delete user through REST API for example<br></div><div><br></div><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></span></body></html>