Create a user with the rest API.

183 views
Skip to first unread message

Paul Bleeker

unread,
Apr 15, 2021, 2:09:11 PM4/15/21
to Keycloak User
I am trying to create a user using the spring REST client. When I pass the user with "rep" key I get: 400 Bad Request: [Unrecognized field "rep" (class org.keycloak.representations.idm.UserRepresentation), not marked as ignorable]

Here is the code:

public String createUser2() throws UnsupportedEncodingException, JSONException {

    String adminToken = getAccessToken();
   
        System.out.println("token: " + adminToken);

HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
headers.set("Authorization", "Bearer " + adminToken);
UserRepresentation userRepresentation = new UserRepresentation();
userRepresentation.setFirstName("some");
userRepresentation.setLastName("user");
userRepresentation.setUsername("Some...@trilliant.com");
userRepresentation.setEmail("Some...@trilliant.com");
Gson gson = new Gson();
String jsonUser = gson.toJson(userRepresentation);
    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("rep", jsonUser);
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        
        String realm = "12xDemo";
        String uri = keycloakUsersUri.replace("$REALM", realm);
        System.out.println("URI: " + uri);
        
        String response = (new RestTemplate()).postForObject(uri, request, String.class);
        JSONObject obj = new JSONObject(response);

        return obj.toString();
    }
 

Paul Bleeker

unread,
Apr 15, 2021, 2:17:18 PM4/15/21
to Keycloak User
Forgot, here's the URI:

private String keycloakUsersUri =  "localhost:8080/auth/admin/realms/$REALM/users"; 
Reply all
Reply to author
Forward
0 new messages