How to generate keycloak token on CREODIAS

../_images/button_orange_cf24.png

a) Using web browser console

  1. Open your web browser and enter https://finder.creodias.eu/www/ in the address field.

../_images/1a.png
  1. Click on Log In button located in the upper-right-corner.

../_images/1.png
  1. Fill in the blanks with your e-mail address and password.

../_images/2.png
  1. Press “Login”.

../_images/3.png
  1. Click somewhere on the site with the right mouse button and choose “Inspect Element”.

../_images/4.png
  1. Change your view side from Inspector to Console.

../_images/5.png
  1. Click on the white space beside the blue arrow to start typing.

../_images/6.png
  1. Type in keycloak.token and press enter in order to generate your token.

../_images/7.png

b) By query with cURL

This case has already been presented in article: How to order products using Finder API on CREODIAS.

CURL is a tool to send data to the server using several protocols such as HTTP. In this example, the output is being filtered by grep and awk commands to obtain a token. In the Linux operating system it’s being seen as environmental variable KEYCLOAK_TOKEN.

export KEYCLOAK_TOKEN=$(curl -d 'client_id=CLOUDFERRO_PUBLIC' \
                             -d "username=${OS_USERNAME}" \
                             -d "password=${OS_PASSWORD}" \
                             -d 'grant_type=password' \
                             'https://identity.cloudferro.com/auth/realms/DIAS/protocol/openid-connect/token' | \
                             python3 -m json.tool | grep "access_token" | awk -F\" '{print $4}')

c) By Python script

import json
import requests

def get_keycloak(username: str, password: str) -> str:
    data = {
        "client_id": "CLOUDFERRO_PUBLIC",
        "username": username,
        "password": password,
        "grant_type": "password",
    }
    try:
        r = requests.post(
            "https://identity.cloudferro.com/auth/realms/DIAS/protocol/openid-connect/token",
            data=data,
        )
        r.raise_for_status()
    except Exception as e:
        raise Exception(
            f"Keycloak token creation failed. Reponse from the server was: {r.json()}"
        )
    return r.json()["access_token"]

keycloak_token = get_keycloak(username, password)