Saturday, April 23, 2022

[SOLVED] Obtaining an access token via cURL

Issue

Simple question:

Why does the following code work... (it returns the access token just fine)

curl --data "grant_type=client_credentials&client_id=synchronization_tool&client_secret=8f6a6e73-66ca-4f8f-1234-ab909147f1cf" http://localhost:8080/auth/realms/master/protocol/openid-connect/token 

And this one doesn't?

curl -d  '{"grant_type":"client_credentials","client_secret":"8f6a6e73-66ca-4f8f-1234-ab909147f1cf","client_id":"synchronization_tool"}' http://localhost:8080/auth/realms/master/protocol/openid-connect/token -H "Content-Type: application/json"

It gives gives me:

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

Aren't they supposed to be two completely analogous requests?


Solution

curl -d 'client_id=xxx' -d 'username=xxx' -d 'password=xxx' -d 'grant_type=password' 'http://localhost:8080/auth/realms/YOUR_REALM_NAME/protocol/openid-connect/token' | python -m json.tool

This works for me, and it will give you the access_token and session_token



Answered By - Anurag Choudhary
Answer Checked By - Terry (WPSolving Volunteer)