keycloakkeycloak-rest-api

How to authenticate in the Keycloak admin api?


So Keycloak has this admin api: https://www.keycloak.org/docs-api/22.0.1/rest-api/index.html#_overview

They do not mention what kind of authentication to use. I tried with Basic, but it does not work.


Solution

  • Access Token Request

    https://datatracker.ietf.org/doc/html/rfc6749#section-4.3.2

    Token Endpoint

    POST http://localhost:8080/realms/{realm}/protocol/openid-connect/token
    
    grant_type: password
    username  : {admin username}
    password  : {admin password}
    client_id : admin-cli
    

    Demo

    1 Install Docker Desktop for Windows or Mac

    https://www.docker.com/products/docker-desktop/
    

    enter image description here

    2 Launch Keycloak by docker-compose

    Save as docker-compose.yml

    version: '3.8'
    
    services:
      postgres:
        image: postgres:15.6
        container_name: postgres_db
        volumes:
          - postgres_data:/var/lib/postgresql/data
        environment:
          POSTGRES_DB: keycloak
          POSTGRES_USER: keycloak
          POSTGRES_PASSWORD: password
    
      keycloak_web:
        image: quay.io/keycloak/keycloak:24.0.3
        container_name: keycloak_web
        environment:
          KC_DB: postgres
          KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak
          KC_DB_USERNAME: keycloak
          KC_DB_PASSWORD: password
    
          KC_HOSTNAME: localhost
          KC_HOSTNAME_STRICT: false
          KC_HOSTNAME_STRICT_HTTPS: false
    
          KC_LOG_LEVEL: info
          KC_METRICS_ENABLED: true
          KC_HEALTH_ENABLED: true
          KEYCLOAK_ADMIN: admin
          KEYCLOAK_ADMIN_PASSWORD: admin
        command: start-dev
        depends_on:
          - postgres
        ports:
          - 8080:8080
    
    volumes:
      postgres_data:
    
    docker compose up -d
    

    enter image description here

    3 Get master token by Postman

    Download and install Postman

    POST http://localhost:8080/realms/master/protocol/openid-connect/token
    

    In body tab Select x-www-form-urlencoded

    Master's admin credential username is admin, password is admin.

    grant_type: password
    username: admin
    password: admin
    client_id: admin-cli
    

    enter image description here

    In Tests tab

    const jsonData = JSON.parse(responseBody);
    postman.setEnvironmentVariable("master-token", jsonData.access_token);
    

    enter image description here

    Press Send button

    enter image description here

    4 Call Admin API by Postman

    Get reamls list

    GET http://localhost:8080/admin/realms
    

    In Authorization tab, Select Bearer Token and enter this text in Token editor control

    {{master-token}}
    

    enter image description here

    You can see the realms list in the response Body enter image description here

    Note

    The master token is valid for only 60 seconds as default.

    You can extend more time for debugging purposes.

    By Browser, open this URL

    http://localhost:8080
    

    Login using the master credential username is "admin" and password is "admin"

    enter image description here