Automating Keycloak with Java

We needed to automate some tasks in our Keycloak so that some elements were created as events were happening.

I find that the Java library for Keycloak is not always intuitive. It does not help that my searches for the documentation or examples returned all sorts of contradicting results.

In this case adding an Identity Provider seems to have many ways to be done but only 1 worked for me.

What works:

destinationRealmResource.identityProviders().create(identityProviderRepresentation);

Alternative code I had seen that I could not get to work for me:

destinationRealmResource.toRepresentation().addIdentityProvider(identityProviderRepresentation);
var clientRes = client.updateRealm(destinationRealmResource.toRepresentation());

And this did not work for me:

destinationRealmResource.toRepresentation().setIdentityProviders(List.of(identityProviderRepresentation));
var clientRes = client.updateRealm(destinationRealmResource.toRepresentation());

This was done with Keycloak 15.

In my troubleshooting I also noticed that having some invalid strings in your representation configuration can cause the creation to fail. I am not a fan of the Map<String, String> for the configuration because that leaves room for a lot of mistakes.

Published by

m5c

Java developper that loves photography and an excellent espresso

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s