java 从 postman 访问 keycloak API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49313554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:19:49  来源:igfitidea点击:

Access the keycloak API from postman

javapostmankeycloakkeycloak-services

提问by Programmer

I have tried to access the keycloak API from the postman. but it is showing 400 bad request.

我试图从邮递员那里访问 keycloak API。但它显示 400 个错误的请求。

I was calling api in the below format.

我正在以以下格式调用 api。

http://{hostname}:8080/auth/realms/master/protocol/openid-connect/token?username=admin&password=admin&client_id=admin-cli&grant_type=password

In the headers I have set the content_type as application/x-www-form-urlencoded

在标题中,我设置了 content_type as application/x-www-form-urlencoded

I am getting the response as below.

我收到如下回复。

{
    "error": "invalid_request",
    "error_description": "Missing form parameter: grant_type"
}

Can any one help me.Any help will be appreciated. thanks in advance

任何人都可以帮助我。任何帮助将不胜感激。提前致谢

回答by UchihaItachi

A bit late for this question, but you did ask about postman and not curl. So you have to put the options in x-www-form-urlencoded enter image description here

这个问题有点晚了,但你确实问了邮递员而不是卷曲。所以你必须把选项放在 x-www-form-urlencoded 在此处输入图片说明

回答by Ankur Singhal

You call API through POST client

您通过 POST 客户端调用 API

URL- http://localhost:8080/auth/realms/Demo/protocol/openid-connect/token

URL- http://localhost:8080/auth/realms/Demo/protocol/openid-connect/token

So here in above url i am using Demoas my realm instead of master.

因此,在上面的 url 中,我将其Demo用作我的领域而不是master.

ContentType- "Content-Type":"application/x-www-form-urlencoded"

ContentType- "Content-Type":"application/x-www-form-urlencoded"

Params:

参数

{

{

"client_secret" : "90ec9638-7647-4e65-ad20-b82df3341084",
"username" : "ankur",
"password" : "123456",
"grant_type" : "password",
"client_id": "app-client"

}

}

Set Header as below

设置标题如下

enter image description here

在此处输入图片说明

Data Need to be passed as shown belowenter image description here

需要传递的数据如下图在此处输入图片说明

回答by Pablo Bastidas

The URL you are using is to obtain the token.

您使用的 URL 用于获取令牌。

The token request should be a POST call, the request you post is a GET request. Below a CURL example about how to request the access_token

令牌请求应该是 POST 调用,您发布的请求是 GET 请求。下面是一个关于如何请求的 CURL 示例access_token

curl -X POST \
   http://{hostname}:8080/auth/realms/{realm}/protocol/openid-connect/token \
   -H 'Content-Type: application/x-www-form-urlencoded' \
   -d 'username=admin&password=admin&grant_type=password&client_id=admin-cli'

回答by Rajat jain

You can also use CURL to get information

也可以使用 CURL 来获取信息

curl -L -X POST 'http://<serveraddress>/auth/realms/<realmname>/protocol/openid-connect/token' -H 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'client_id=<clientid>' --data-urlencode 'grant_type=password' --data-urlencode 'client_secret=<clientsecret>' --data-urlencode 'scope=openid' --data-urlencode 'username=<username>' --data-urlencode 'password=<password>'