How can I update a existing Keycloak client attribute in a realm?

I've added few attributes for each client in keycloak, and now I need to update the existing client attribute to new value without deleting the client.

List<ClientRepresentation> list = realm.clients().findByClientId(client.getClientId());
ClientResource resource = realm.clients().get(client.getClientId());

ClientRepresentation clientRepresentation = list.get(0);
        //clientRepresentation.getAttributes().put(KeyCloakAttribute.OBJECT_BASED_ACCESS.getLabel(), application.getAttributes().getObjectBasedModel());
clientRepresentation.setName("updated-name");
resource.update(clientRepresentation); <- This line is throwing error

I could see client resource is providing update method, but when i use it i'm getting 404 error.

2022-01-22 19:07:29.057  -ERROR 15870 [SpringApplication.java] [reportFailure] [org.springframework.boot.SpringApplication]:826 --- [restartedMain] ngp-dev-env o.s.boot.SpringApplication : Application run failed javax.ws.rs.NotFoundException: HTTP 404 Not Found
at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:225)
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.$Proxy229.getServiceAccountUser(Unknown Source)
at com.service.KeycloakApplication.updateAttributes(KeycloakApplication.java:89)
at com.service.RegisterService.register(RegisterService.java:32)
at com.OpenAPI2SpringBoot.registerApplication(OpenAPI2SpringBoot.java:88)
at com.OpenAPI2SpringBoot.onApplicationEvent(OpenAPI2SpringBoot.java:80)
at com.OpenAPI2SpringBoot.onApplicationEvent(OpenAPI2SpringBoot.java:1)
at com.OpenAPI2SpringBoot$$EnhancerBySpringCGLIB$$a66f061b.onApplicationEvent(<generated>)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360)
at org.springframework.boot.context.event.EventPublishingRunListener.running(EventPublishingRunListener.java:103)
at org.springframework.boot.SpringApplicationRunListeners.running(SpringApplicationRunListeners.java:77)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
at com.OpenAPI2SpringBoot.main(OpenAPI2SpringBoot.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

keycloak client resource update docs

am I doing it in the wrong way or is there any other way to do it ?


According to the API documentation realm.clients() returns a ClientsResource interface. The method findByClientId requests the clientId as parameter but the method get:

@Path(value="{id}")
ClientResource get(@PathParam(value="id")
                                       String id)

requests the id of the client, not the client ID.

So you need to replace:

realm.clients().get(client.getClientId());

with

realm.clients().get(client.getId());