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.
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.