node.jskeycloakkeycloak-serviceskeycloak-rest-api

How can I add user consent with create user or other API(s)?


I am using keycloak API:

http://localhost:8080/auth/admin/realms/master/users for creating user

it's working fine but I need to add user consent with this API or by any alternative API(s), How can I do this,

Thanks in advance.


Solution

  • You can try the following, although I am not 100% it will work:

    If you look at the Keycloak Admin REST API you can see that for the endpoint:

    POST /{realm}/users
    

    which is the endpoint that you are using to create the users you can pass the following body parameters:

    UserRepresentation
    

    which is composed by (among others) the following (optional) field :

    clientConsents <UserConsentRepresentation> array
    

    Therefore, you can create the user already with its consents.

    I would suggested you to first create a dummy user, create the consents via Admin Console, then call:

    GET /{realm}/users/{id} 
    

    inspect how the response looks, so that you know how the body content of the POST /{realm}/users should look like.