Set Password for user from Java SDK

Can I set a user’s password at anytime as “admin/service” account? I am using the Java SDK and referencing [Users | Okta Developer](Set Password)

Below is the Groovy code:

Client client = Clients.builder()
    .setOrgUrl("https://${domain}")
    .setClientCredentials(new TokenClientCredentials("${apiToken}"))
    .build()

UserList users = client.listUsers(user.getEmail(), null, null, null, null)
User user1 = users?.first()
String userId = user1?.getId()

Resource userPasswordRequest = client.instantiate(ExtensibleResource)
userPasswordRequest.put("credentials", client.instantiate(ExtensibleResource)
    .put("password", client.instantiate(ExtensibleResource)
    .put("value", password.toCharArray())))
Resource result = client.getDataStore().http()
    .setBody(userPasswordRequest)
    .post("/api/v1/users/"+ userId, User.class)
log.info("Result: ${result}")

I did not get any errors but the password is unchanged.

Is there a reason you’re not using the UserCredentials setPassword method to do this? This one should work for a super admin to be able to set the password for any user.

@Andrea thanks for the suggestion, the code below worked when creating a User. I will try using UserCredentials.setPassword method.

        User user = UserBuilder.instance()
        .setEmail(email)
        .setLogin(username)
        .setFirstName(firstName)
        .setLastName(lastName)
        .setActive(active)
        .setBcryptPasswordHash(passwordHash, salt, rounds)
        .buildAndCreate(client)
        PasswordCredential passwordCredential = client.instantiate(PasswordCredential.class)
            .setValue("NewPassword-!#123".toCharArray());
        user.getCredentials().setPassword(passwordCredential);
        user.update();
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.