With a simple GET REST-Request, you can get a token you can use for later requests
GET "grant_type=password&client_id=admin-cli&username=$USERNAME&password=$PASSWORD" "$HOST/auth/realms/$REALM/protocol/openid-connect/token"
as bash (jq must be installed):
#get admin bearer token
AUTH_RESPONSE_KC_ADMIN=$(curl --silent -d "grant_type=password&client_id=admin- cli&username=$USERNAME&password=$PASSWORD" "$HOST/auth/realms/master/protocol/openid-connect/token")
if [[ $AUTH_RESPONSE_KC_ADMIN != *"access_token"* ]]; then
echo "No access token for keycloak admin!"
echo $AUTH_RESPONSE_KC_ADMIN
exit -1
fi
TOKEN_KC_ADMIN=$(echo $AUTH_RESPONSE_KC_ADMIN | jq -r '.access_token')
AUTH_TOKEN_KC_ADMIN="Authorization: Bearer $TOKEN_KC_ADMIN"
But this access token is only valid for some time (depending on keycloak settings)
To create a client with REST:
#use: create_client clientName
function create_client {
CLIENT='{"enabled":true,"attributes":{},"redirectUris":["*"],"clientId":"'$1'","protocol":"openid-connect", "secret":"'$SECRET'","clientAuthenticatorType":"client-secret","publicClient":"false"}'
curl -i --silent -d "$CLIENT" -H "$AUTH_TOKEN_KC_ADMIN" -H "$CONTENT_TYPE" $HOST/auth/admin/realms/$REALM/clients | head -1
}