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

新手上路,请多包涵

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

在文档中我看到:

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 错误。

我做错了什么?

原文由 president 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 685
2 个回答

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

  1. 你需要一个令牌。您可以使用 OpenID 生成(请参阅 文档)。
  2. 使用令牌(通过标头授权),您可以向 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"})

原文由 Marcos Pereira Júnior 发布,翻译遵循 CC BY-SA 4.0 许可协议

对于公共客户,您无法获得 client_secret 。你的客户应该有 ‘access_type` = ‘confidential’

  1. 转到领域管理面板的 CLIENTS 部分 ( <protocol>://<host>:<port>/auth/admin/master/console/#/realms/<your realm>/clients/<your client code> )
  2. 将访问类型更改为 confidential 机密的
  3. 按“保存”
  4. 转到“凭据”选项卡
  5. 确保 ‘Client Authenticator’ = ‘Client Id and Secret’
  6. 瞧!这是您的客户机密:

秘密

UPD PS client_secret 可以 通过另一个客户端 使用 API 进行检索 (它应该具有客户端信息视图的作用)

原文由 xardbaiz 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题