Keycloak 缺少表单参数:grant_type
Keycloak Missing form parameter: grant_type
我的本地机器上有独立的 keycloak 运行。
我创建了名为 'spring-test' 的新领域,然后创建了名为 'login-app'
的新客户端
根据其余文档:
POST: http://localhost:8080/auth/realms/spring-test/protocol/openid-connect/token
{
"client_id": "login-app",
"username": "user123",
"password": "pass123",
"grant_type": "password"
}
应该给我 jwt 令牌,但我收到错误的响应请求
{
"error": "invalid_request",
"error_description": "Missing form parameter: grant_type"
}
我假设我的配置中缺少某些东西。
编辑:
我使用的是 json body 但它应该是 application/x-www-form-urlencoded
:
以下 body 有效:
token_type_hint:access_token&token:{token}&client_id:{client_id}&client_secret:{client_secret}
您应该在 POST 请求中发送您的数据,并将 Content-Type
header 值设置为 application/x-www-form-urlencoded
,而不是 json。
对于 curl 有问题的人,curl 命令如下
curl -d "client_secret=<client-secret>" -d "client_id=<client-id>" -d "username=<username>" -d "password=<password>" -d "grant_type=password" "http://localhost:8080/auth/realms/<realm-name>/protocol/openid-connect/token"
curl 命令在没有 Content-Type
header.
的情况下工作
致那些因搜索 JavaScript
解决方案而来到这里的人。
这里是使用 axios
.
以 keycloak
权限将 code
交换为 access_token
的示例
本例中使用
npm install querystring
或
yarn add querystring
发送请求:
import queryString from 'querystring'
const params = {
grant_type: 'authorization_code',
client_id: 'client-id-here',
code: 'code-from-previous-redirect',
redirect_uri: location.protocol + '//' + location.host
};
axios({
method: 'post',
url: 'https://my-keycloak.authority/token',
data: queryString.stringify(params),
config: {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}
}).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
You are required to send a POST request with the parameters as a URL encoded string in the request body.
FormData object does not work.
这是一个示例 CURL 命令
curl -X POST \
http://localhost:8080/auth/realms/your_realm_name/protocol/openid-connect/token \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 69' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: KC_RESTART=' \
-H 'Host: localhost:8080' \
-H 'Postman-Token: 88f38aa0-8659-4b37-a2aa-d6b92177bdc2,29c4e7db-51f4-48d1-b6d5-daab06b68ab4' \
-H 'User-Agent: PostmanRuntime/7.20.1' \
-H 'cache-control: no-cache' \
-d 'client_id=my-app&username=admin&password=admin123&grant_type=password'
我在 SOAPUI 测试中遇到了类似的问题。我们不应该POST一个json。当我清除 'Media Type' 并选中 'PostQueryString' 复选框时,这个问题得到了解决。 'Media Type box' 将自行设置为 'www-form-urlencoded'。通过点击加号在顶部添加属性。
有卷曲
curl -X POST \
http://localhost:8080/auth/realms/api-gateway/protocol/openid-connect/token \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 73' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: JSESSIONID=F8CD240FF046572F864DC37148E51128.a139df207ece; JSESSIONID=65D31B82F8C5FCAA5B1577DA03B4647C' \
-H 'Host: localhost:8080' \
-H 'Postman-Token: debc4f90-f555-4769-b392-c1130726a027,d5087d9f-9253-48bd-bb71-fda1d4558e4d' \
-H 'User-Agent: PostmanRuntime/7.15.2' \
-H 'cache-control: no-cache' \
-d 'grant_type=password&client_id=api-gateway&username=admin&password=temp123'
邮递员
(Select x-www-form-urlencoded 参数选项)
我的本地机器上有独立的 keycloak 运行。
我创建了名为 'spring-test' 的新领域,然后创建了名为 'login-app'
的新客户端根据其余文档:
POST: http://localhost:8080/auth/realms/spring-test/protocol/openid-connect/token
{
"client_id": "login-app",
"username": "user123",
"password": "pass123",
"grant_type": "password"
}
应该给我 jwt 令牌,但我收到错误的响应请求
{
"error": "invalid_request",
"error_description": "Missing form parameter: grant_type"
}
我假设我的配置中缺少某些东西。
编辑:
我使用的是 json body 但它应该是 application/x-www-form-urlencoded
:
以下 body 有效:
token_type_hint:access_token&token:{token}&client_id:{client_id}&client_secret:{client_secret}
您应该在 POST 请求中发送您的数据,并将 Content-Type
header 值设置为 application/x-www-form-urlencoded
,而不是 json。
对于 curl 有问题的人,curl 命令如下
curl -d "client_secret=<client-secret>" -d "client_id=<client-id>" -d "username=<username>" -d "password=<password>" -d "grant_type=password" "http://localhost:8080/auth/realms/<realm-name>/protocol/openid-connect/token"
curl 命令在没有 Content-Type
header.
致那些因搜索 JavaScript
解决方案而来到这里的人。
这里是使用 axios
.
keycloak
权限将 code
交换为 access_token
的示例
本例中使用
npm install querystring
或
yarn add querystring
发送请求:
import queryString from 'querystring'
const params = {
grant_type: 'authorization_code',
client_id: 'client-id-here',
code: 'code-from-previous-redirect',
redirect_uri: location.protocol + '//' + location.host
};
axios({
method: 'post',
url: 'https://my-keycloak.authority/token',
data: queryString.stringify(params),
config: {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}
}).then(response => {
console.log(response.data);
}).catch(error => {
console.error(error);
});
You are required to send a POST request with the parameters as a URL encoded string in the request body.
FormData object does not work.
这是一个示例 CURL 命令
curl -X POST \
http://localhost:8080/auth/realms/your_realm_name/protocol/openid-connect/token \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 69' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: KC_RESTART=' \
-H 'Host: localhost:8080' \
-H 'Postman-Token: 88f38aa0-8659-4b37-a2aa-d6b92177bdc2,29c4e7db-51f4-48d1-b6d5-daab06b68ab4' \
-H 'User-Agent: PostmanRuntime/7.20.1' \
-H 'cache-control: no-cache' \
-d 'client_id=my-app&username=admin&password=admin123&grant_type=password'
我在 SOAPUI 测试中遇到了类似的问题。我们不应该POST一个json。当我清除 'Media Type' 并选中 'PostQueryString' 复选框时,这个问题得到了解决。 'Media Type box' 将自行设置为 'www-form-urlencoded'。通过点击加号在顶部添加属性。
有卷曲
curl -X POST \
http://localhost:8080/auth/realms/api-gateway/protocol/openid-connect/token \
-H 'Accept: */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Content-Length: 73' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Cookie: JSESSIONID=F8CD240FF046572F864DC37148E51128.a139df207ece; JSESSIONID=65D31B82F8C5FCAA5B1577DA03B4647C' \
-H 'Host: localhost:8080' \
-H 'Postman-Token: debc4f90-f555-4769-b392-c1130726a027,d5087d9f-9253-48bd-bb71-fda1d4558e4d' \
-H 'User-Agent: PostmanRuntime/7.15.2' \
-H 'cache-control: no-cache' \
-d 'grant_type=password&client_id=api-gateway&username=admin&password=temp123'
邮递员 (Select x-www-form-urlencoded 参数选项)