'Add protocol mapper to client via keycloak REST API

By the latest Keycloak's REST API documentation https://www.keycloak.org/docs-api/18.0/rest-api/index.html#_protocol_mappers_resource, I can't find enough information to add a new protocol mapping with mapper type 'Audience' by REST API. I am able to do it by Keycloak UI (See attached) and now I would like to automate it to our DevOps pipeline. enter image description here

I wonder if the only way to find out the json content is to look at the request payload of the POST Request suggested by this post: Where are all of the Keycloak Protocol Mapper Config Options documented?

There are also some information on keycloak ui : in the browser's debugger console (mapper types)

enter image description here



Solution 1:[1]

You need to issue an HTTP POST request to http://<host>:<port>/admin/realms/<realm_name>/clients/<client_id>/protocol-mappers/models

with a payload like this

{
  "protocol":"openid-connect",
  "config": {
    "id.token.claim":"false",
    "access.token.claim":"true",
    "included.client.audience":"admin-cli",
    "included.custom.audience":"custom_audience"
  },
  "name":"Audience Mapper",
  "protocolMapper":"oidc-audience-mapper"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sventorben