如何通过Keycloak API获取客户端机密? [英] How to get client secret via Keycloak API?

查看:387
本文介绍了如何通过Keycloak API获取客户端机密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过Keycloak API获取客户端机密?

How to get client secret via Keycloak API?

在文档中,我看到:

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

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

我的代码如下:

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"})

我总是收到401错误.

I always get 401 error.

我该怎么办?

推荐答案

我认为您的身份验证不起作用.

I think your authentication it's not working.

  1. 您需要一个令牌.您可以使用OpenID生成(请参见 docs ).
  2. 使用令牌(通过标头授权),您可以向API提出请求.
  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.

示例:

获取令牌

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)

请求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"})

这篇关于如何通过Keycloak API获取客户端机密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆