sCHween
sCHween

Reputation: 123

Keycloak create new user fails

Try to create new user with the following code:

Keycloak kc = Keycloak.getInstance(
"http://192.168.11.55:8080/auth", 
"master", // the realm to log in to
"admin", "pass",  // the user
"security-admin-console");

CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue("test123");
UserRepresentation user = new UserRepresentation();
user.setUsername("testuser");
user.setFirstName("Test");
user.setLastName("User");
user.setCredentials(Arrays.asList(credential));
kc.realm("master").users().create(user);

It returns a HTTP 400 Bad Request. Keycloak log says:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "origin" (class     org.keycloak.representations.idm.UserRepresentation), not marked as ignorable (22 known properties: "federatedIdentities", "enabled",     "lastName", "emailVerified", "clientConsents", "self", "socialLinks", "applicationRoles", "createdTimestamp", "groups", "username",     "attributes", "id", "firstName", "email", "federationLink", "serviceAccountClientId", "requiredActions", "realmRoles", "clientRoles",     "totp", "credentials"])
 at [Source: io.undertow.servlet.spec.ServletInputStreamImpl@250fdbe0; line: 1, column: 37] (through reference chain:     org.keycloak.representations.idm.UserRepresentation["origin"])    

I'm using Keycloak 2.3.0.Final and Keycloak Admin REST Client 2.4.0.Final API.

Upvotes: 2

Views: 2794

Answers (2)

user3022479
user3022479

Reputation: 25

It's also worth calling out that in certain older versions of Keycloak, it's not possible to both create a new user, and set the credential on the same kc.realm("master").users().create(user); call.

This can be worked around by first creating the user and not calling setCredentials, and then setting them on another call.

Upvotes: 0

ravthiru
ravthiru

Reputation: 9623

Your KeyCloak Server and Keycloak Admin REST Client should be of the same version. Some fields could have newly added in 2.4.0.Final version.

Upvotes: 3

Related Questions