<div dir="ltr">
<p class="">I am getting 401 unAuthorized when im trying to delete a user.I was able to create the user with this id.And i am able to delete the user using Advanced REST Chrome client using same DELETE URL.</p><p class="">Can you guide what am i doing wrong here.</p><p class=""><br></p><p class="">Thanks</p><p class=""><br></p><p class="">*****************ERROR***********************************************************</p><p class=""><span class="">org.springframework.web.client.HttpClientErrorException</span><span class="">: 401 Unauthorized</span></p><p class=""><span class="">        </span>at org.springframework.web.client.DefaultResponseErrorHandler.handleError(<span class="">DefaultResponseErrorHandler.java:91</span>)</p><p class=""><span class="">        </span>at org.springframework.web.client.RestTemplate.handleResponse(<span class="">RestTemplate.java:641</span>)</p><p class=""><span class="">        </span>at org.springframework.web.client.RestTemplate.doExecute(<span class="">RestTemplate.java:597</span>)</p><p class=""><span class="">        </span>at org.springframework.web.client.RestTemplate.execute(<span class="">RestTemplate.java:557</span>)</p><p class=""><span class="">        </span>at org.springframework.web.client.RestTemplate.delete(<span class="">RestTemplate.java:429</span>)</p><p class=""><span class="">        </span>at com.snrapps.mwp.domain.security.KeyCloakAdminAdapter.deleteUser(<span class="">KeyCloakAdminAdapter.java:256</span>)</p><p class=""><span class="">
</span></p><p class=""><span class="">        </span>at com.snrapps.mwp.domain.security.KeyCloakAdminAdapter.main(<span class="">KeyCloakAdminAdapter.java:55</span>)</p><p class=""><span class="">*****************CODE**************************************************************</span></p><p class=""><span class="">        </span><span class="">public</span> <span class="">boolean</span> deleteUser(String <span class="">userId</span>) {</p>
<p class=""><span class="">        </span><span class="">        </span>AccessTokenResponse <span class="">accessTokenResponse</span> = getToken();</p>
<p class="">org.springframework.http.HttpEntity<String> <span class="">entity</span> = <span class="">new</span> org.springframework.http.HttpEntity<String>(getHeaders(<span class="">accessTokenResponse</span>.getToken()));</p>
<p class=""><span class="">        </span><span class="">        </span>HttpHeaders <span class="">httpHeaders</span>=<span class="">entity</span>.getHeaders();</p>
<p class=""><span class="">        </span><span class="">        </span>System.<span class="">out</span>.println(<span class="">httpHeaders</span>);</p>
<p class=""><span class="">        </span><span class="">        </span>RestTemplate <span class="">restTemplate</span> = <span class="">new</span> RestTemplate();</p>
<p class=""><span class=""><span class="">        </span><span class="">        </span>String </span><span class="">urlForDeleteUser</span><span class=""> = </span>"http://XXXXX.com:XXXX/auth/admin/realmsMyAppsRealm/users/"<span class=""> + </span><span class="">userId</span><span class="">;</span></p>
<p class=""><span class="">        </span><span class="">        </span>System.<span class="">out</span>.println(<span class="">urlForDeleteUser</span>);</p>
<p class=""><span class=""><span class="">        </span><span class="">        </span></span>restTemplate<span class="">.delete(</span>urlForDeleteUser<span class="">, </span>entity<span class="">);</span></p>
<p class=""><span class="">        </span><span class="">        </span></p>
<p class=""><span class="">        </span><span class="">        </span></p>
<p class=""><span class="">        </span><span class="">        </span></p>
<p class=""><span class="">        </span><span class="">        </span></p>
<p class=""><span class="">        </span><span class="">        </span>System.<span class="">out</span>.println(<span class="">"done"</span>);</p>
<p class=""><br></p>
<p class=""><span class=""><span class="">        </span><span class="">        </span></span>return<span class=""> </span>true<span class="">;</span></p>
<p class=""><span class="">        </span>}</p></div>