'how to create a new user with postman and assign group in keycloak?

I have this JSON to create a new user, which works for me, but it doesn't assign the group declared in the groups "groups":["APIs"] tab to the user

json:

{ 
   "username":"[email protected]",
   "firstName":"1pruebattributesFN",
   "lastName":"1pruebattributesAP",
   "email":"[email protected]",
   "enabled":true,
   "groups":["APIs"],
   "credentials":[ 
      { 
         "temporary": false,
         "type":"password",
         "value":"1234"
      }
   ],
   "attributes":
      { 
         "EmailVerifiedvn":"TRUE",
         "Enabledvn":"TRUE",
         "Entity":"[{\"id\":\"3411982108\",\"name\":\"3411982108 - Claro Home  PAGO APP 2\"}]",
         "State":"Activo",
         "UserCreateDate":"202001031650Z",
         "UserLastModifiedDate":"202001031655Z",
         "UserStatus":"CONFIRMED"
      }
}

the user is created but does not assign the group to the user.

in my keyclok I have declared 3 groups:

groups created

thanks.



Solution 1:[1]

I don't think it works that way, you can use below API to assign a user to a group:

    Request URL: http://localhost:8080/auth/admin/realms/{realm}/users/{userId}/groups/{groupId}
    Request Method: PUT

Source: Keycloak Admin Rest API Docs

Solution 2:[2]

In keycloak 17 it can be assigned directly:

First you have to create the group "mygroup" inside Keycloak

POST
http://<ip>:8080/admin/realms/master/users

Header

Name: Authorization
Value: Bearer <Admin cli Access token>

Body raw data

{"enabled":true,"username":"usergrupo","email":"[email protected]","firstName":"functest","lastName":"functest", "groups":["migrupo", "miotrogrupo"], "credentials":[{"type":"password","value":"123","temporary":false}]}

Finally the associated user will be created in that group

Documentation:

https://www.keycloak.org/docs-api/12.0/rest-api/#_userrepresentation

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 Naman Parashar
Solution 2