HTTP request to keycloak with insommia

69 Views Asked by At

I am creating a secured back-end for a new app with Keycloak and NestJS. I imported my realm with clients and user in it and I am trying to get the token and access protected data through Insomnia.

Trying to reach endpoint {{ _.baseUrl }}/protocol/openid-connect/auth with a POST request passing data through JSON is returning

keycloak-1  | 2024-03-11 16:01:56,436 WARN  [org.keycloak.protocol.oidc.endpoints.request.AuthorizationEndpointRequestParserProcessor] (executor-thread-131) Parameter 'client_id' not present or present multiple times in the HTTP request parameters
keycloak-1  | 2024-03-11 16:01:56,437 WARN  [org.keycloak.events] (executor-thread-131) type="LOGIN_ERROR", realmId="55542514-fc71-4e4e-be3e-42af869de2a7", clientId="null", userId="null", ipAddress="172.18.0.1", error="invalid_request"

in my Docker Keycloak logs and a 400 Bad request error in Insomnia response preview window.

At first I was only passing my credentials and the error made me rethink the whole thing and pass client_id and client_secret as well i the JSON data yet error i still the same.

I was following this tutorial but it does not seem suitable for a Keycloak environment

How to reach to my Keycloak endpoints via Insomnia and furthermore how to get more verbose logs in the future ?

1

There are 1 best solutions below

6
Bench Vue On BEST ANSWER

Insomina is a little hard.

Overview

enter image description here

#0 Keycloak launching Detail in here

Access Token URL

POST "http://localhost:8180/realms/master/protocol/openid-connect/token"

How to find Token URL?

enter image description here

The token_endpoint should be match Insomnia URL PREVIEW

enter image description here

If not, 404 not Found Error

#1 Crete New Collection

enter image description here

#2 Name as Keycloak

enter image description here

#3 Click Gear Icon for setting

enter image description here

#4 Base Environment

{
    "baseUrl": "http://localhost:8180",
    "username": "admin",
    "password": "admin",
    "access-token": ""
}

enter image description here

#5 Add HTTP Request

enter image description here

#6-7 Create Get-Token by POST API

enter image description here

#8 CTRL + Space Can add get environment variable

detail in here

enter image description here

#9 Make URL with an environment variable

{{ _.baseUrl }}/realms/master/protocol/openid-connect/token

enter image description here

#10 Add Body key/value by Form URL Encoded

enter image description here

enter image description here

It is same as /x-www-form-urlencoded in Postman

enter image description here

#11~13 Add 4 keys/value

User name and password get from environmental variable

By CTRL + Space

enter image description here

enter image description here

#14 Get token Click Send

Result

enter image description here

Bonus

How to use an access-token?

Add Request Get user list enter image description here

#1 Open Base Environment In the access-token place cursor inside ""

CTRL+Space

Select f Response => Body Attribute

enter image description here

Result

enter image description here

#2 Click reponse 'body', '', '', 'never',60 enter image description here

Select: Request POST Get Token

enter image description here

Filter (JSONPath or XPath)

$.access_token

enter image description here

Result enter image description here

enter image description here

Finally, get user list with access-token

enter image description here