title: Keycloak creating user for an existing realm?
body: <p>I'm trying to create a user for an existing realm and client. Below, is what i tried. The URL, realm, username, password, clientId and clientSecret is correct. But it throws <code>NotAuthorization</code> exception on line <code>var response = keycloak.realm( realm ).users().create( user );</code> I also tried to get it with authorization toke but it threw the same error. What am i doing wrong?</p>
  <pre class="lang-java prettyprint-override"><code>        Keycloak keycloak = Keycloak.getInstance( serverUrl, realm, username, password, clientId, clientSecret );
  
          var user = new UserRepresentation();
          user.setEnabled( true );
          user.setUsername( &quot;test&quot; );
          user.setAttributes(Collections.singletonMap(&quot;origin&quot;, Collections.singletonList( &quot;demo&quot; ) ));
  
          CredentialRepresentation credentialRepresentation = new CredentialRepresentation();
          credentialRepresentation.setTemporary( false );
          credentialRepresentation.setType( CredentialRepresentation.PASSWORD );
          credentialRepresentation.setValue( &quot;1234&quot; );
          user.setCredentials( Collections.singletonList( credentialRepresentation ) );
  
          try {
              var response = keycloak.realm( realm ).users().create( user );
              System.out.println( &quot;Response: &quot; + response.getStatus() );
              System.out.println( &quot;Response: &quot; + response.getMetadata() );
  
          } catch ( Exception e ) {
              e.printStackTrace();
          }
  </code></pre>
  <p>The error stack trace:</p>
  <pre><code>javax.ws.rs.ProcessingException: javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
      at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterRequest(ClientInvocation.java:603)
      at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:440)
      at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:149)
      at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
      at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
      at com.sun.proxy.$Proxy26.create(Unknown Source)
      at io.heist.keycloak.user.generator.KeycloakGen.main(KeycloakGen.java:27)
  Caused by: javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
      at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:221)
      at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:195)
      at org.jboss.resteasy.client.jaxrs.internal.proxy.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:62)
      at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invokeSync(ClientInvoker.java:151)
      at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:112)
      at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:76)
      at com.sun.proxy.$Proxy16.grantToken(Unknown Source)
      at org.keycloak.admin.client.token.TokenManager.grantToken(TokenManager.java:90)
      at org.keycloak.admin.client.token.TokenManager.getAccessToken(TokenManager.java:70)
      at org.keycloak.admin.client.token.TokenManager.getAccessTokenString(TokenManager.java:65)
      at org.keycloak.admin.client.resource.BearerAuthFilter.filter(BearerAuthFilter.java:52)
      at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.filterRequest(ClientInvocation.java:590)
      ... 6 more
  
  Process finished with exit code 0
  </code></pre>
  <p>Related dependencies:</p>
  <pre><code>        &lt;dependency&gt;
              &lt;groupId&gt;org.keycloak&lt;/groupId&gt;
              &lt;artifactId&gt;keycloak-core&lt;/artifactId&gt;
              &lt;version&gt;10.0.2&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.keycloak&lt;/groupId&gt;
              &lt;artifactId&gt;keycloak-admin-client&lt;/artifactId&gt;
              &lt;version&gt;11.0.1&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>