首页 > 解决方案 > 从 Keycloak 令牌端点获取授权码

问题描述

在 keycloak 文档中写道,令牌端点可用于 在授权代码流中获取临时代码,或用于通过隐式流、直接授予或客户端授予获取令牌。

但即使使用 response_type=code ,我也无法获得授权码:只有一个令牌。我怎样才能做到这一点?

我的测试要求:

curl -X POST \
  http://localhost:8080/auth/realms/my-realm/protocol/openid-connect/token \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Host: localhost:8080' \
  -H 'Postman-Token: e103dff9-7b25-4f8f-886b-2af73efee561,e8f92a85-1489-4d7f-b89f-76cfe85e9c68' \
  -H 'User-Agent: PostmanRuntime/7.15.0' \
  -H 'accept-encoding: gzip, deflate' \
  -H 'cache-control: no-cache' \
  -H 'content-length: 94' \
  -d 'grant_type=password&username=login&password=pwd&client_id=my-app&response_type=code'

来源:https ://www.keycloak.org/docs/latest/server_admin/index.html#keycloak-server-oidc-uri-endpoints

标签: oauth-2.0keycloakopenid-connect

解决方案


response_type只能在对授权端点(http://localhost:8080/auth/realms/my-realm/protocol/openid-connect/auth)的授权请求中使用,在这种情况下(在令牌请求中)将被忽略。授权码可以从授权端点获取,如下所示:

http://localhost:8080/auth/realms/my-realm/protocol/openid-connect/auth?client_id=my-ap&redirect_uri=https://...&response_type=code

另见: https ://www.rfc-editor.org/rfc/rfc6749#section-4.1.1


推荐阅读