Option1: Service Accounts
You need to toggle Service Account Enabled button in the client application settings and then you can get a token using client_credentials grant.

Let’s get token using the below curl command:
curl --location --request POST 'http://localhost:8181/auth/realms/education/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Authorization: Basic base64(clientId:clientSecret)' \
--data-urlencode 'grant_type=client_credentials'
language-json
Response
{
"access_token": "a jwt token",
"expires_in": 300,
"refresh_expires_in": 0,
"token_type": "Bearer",
"not-before-policy": 0,
"scope": "email profile"
}
language-json
Note: this token is one time token and can’t be refreshed.
Let’s assign a functionality “ view-users ” and “manage-users” to the service account.
We can find this role under “Client Roles” → Realm-management
Click on “view-users”, “manage-users” Role in available roles and assign it to the service account as shown in the image below. This will assign the role of viewing the user list and managing the users to the our client application in the keycloak realm “education”.

Let’s create a user with the token we got above:
curl --location --request POST 'http://localhost:8181/auth/admin/realms/education/users' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer {{access_token}}' \
--data-raw '{
"createdTimestamp": 1588880747548,
"username": "samir",
"enabled": true,
"totp": false,
"emailVerified": true,
"firstName": "fName",
"lastName": "Lname",
"email": "someemail@gmail.com",
"disableableCredentialTypes": [],
"requiredActions": [],
"notBefore": 0,
"access": {
"manageGroupMembership": true,
"view": true,
"mapRoles": true,
"impersonate": true,
"manage": true
},
"realmRoles": [ "mb-user" ]
}'
language-json
Response
201 Created.
Let’s get the user list :
curl --location --request GET 'http://localhost:8181/auth/admin/realms/education/users' \
--header 'Authorization: Bearer {{access_token}}'
language-json
Response
[
{
"id": "047add83-d1de-456a-b0a7-8480292fb769",
"createdTimestamp": 1635842744523,
"username": "adf",
"enabled": true,
"totp": false,
"emailVerified": true,
"firstName": "sdf",
"lastName": "sdf",
"email": "somemail@gmail.com",
"disableableCredentialTypes": [],
"requiredActions": [],
"notBefore": 0,
"access": {
"manageGroupMembership": true,
"view": true,
"mapRoles": true,
"impersonate": false,
"manage": true
}
}
]
language-json
Thus, we have tested the functionality of getting the list of users in the realm by means of service account. Similarly, we can perform various other functions using the service accounts.
Option2 :
We can also achieve the same behavior bu using password grant type and a user with a proper role mapping.So having a client application is required but without service account enabled.
Get token request :
curl --location --request POST 'http://localhost:8181/auth/realms/education/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Authorization: Basic base64(clientId:clientSecret)' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=uname' \
--data-urlencode 'password=pass'
language-json
Response:
{
"access_token": "a jwt token",
"expires_in": 300,
"refresh_expires_in": 1800,
"refresh_token": "refresh jwt token ",
"token_type": "Bearer",
"not-before-policy": 0,
"session_state": "6df504f6-399a-4efd-a23e-067b161e5911",
"scope": "email profile"
}
language-json

After getting access token we can fetch the users using the same token.
curl --location --request GET 'http://localhost:8181/auth/admin/realms/fyrefish-dev/users' \
--header 'Authorization: Bearer {{access_token}}'
language-json
Conclusion
For the Option1 creating user and user session is not required, just having a client application with service accounts enabled will do the things for us .(of course proper roles will be required depending on use case, view/manage users etc).
For the Option2 - we will need a client application + a user with credentials and the proper role mapping. Once we execute /token request keycloak server will create a session for this user.