Answer a question

How to get client secret via Keycloak API?

In documentation I see:

GET /admin/realms/{realm}/clients/{id}/client-secret

My code is the following:

data = {
    "grant_type" : 'password',
    "client_id" : 'myclientid',
    "username" : 'myusername',
    "password" : 'mypassword'
}
response = requests.get("https://mylink.com/auth/admin/realms/{myrealm}/clients/{myclientid}/client-secret", data=data, headers= {"Content-Type": "application/json"})

I always get 401 error.

What do I do wrong?

Answers

I think your authentication it's not working.

  1. You need a token. You can generate using OpenID (see docs).
  2. With the token (by header Authorization), you can do request to API.

Example:

Get the token

data = {"username": "username", "password": "password",
        "client_id": "client_id", "client_secret": "client_secret", 
        "grant_type": "password"}

token = request.post("https://{server-url}/"realms/{realm-name}/protocol/openid-connect/token", data=data)

Request to API

response = requests.get("https://mylink.com/auth/admin/realms/{myrealm}/clients/{myclientid}/client-secret", data=data, headers= {"Authorization": "Bearer " + token.get('access_token'), "Content-Type": "application/json"})
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐