Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining an access token via cURL

Tags:

curl

keycloak

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?

like image 882
dafero Avatar asked Dec 03 '22 20:12

dafero


2 Answers

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

like image 115
Anurag Choudhary Avatar answered Dec 26 '22 20:12

Anurag Choudhary


Curl Command:

curl \
  -d "client_id=account" \
  -d "client_secret=d5141c8b-ea12-4320-a500-74a046083c08" \
  -d "grant_type=client_credentials" \
  "http://localhost:9080/auth/realms/myrealm/protocol/openid-connect/token"

Response:

{
  "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJvd0xKUUZXbmVldV9ORm9WOUZKNkVrQW84SDVfN3RReDc2RTEyRnR3YkhzIn0.eyJleHAiOjE2NDc2NTQzNTAsImlhdCI6MTY0NzY1MDc1MCwianRpIjoiMzU3NGRmN2YtMjFjMi00OGUxLTk2NDUtYjVjNGM4ODM4MjNkIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo5MDgwL2F1dGgvcmVhbG1zL215cmVhbG0iLCJhdWQiOiJzZWN1cml0eS1hZG1pbi1jb25zb2xlIiwic3ViIjoiZjhhMjQ4YjUtZDkyNS00N2RiLTlmYTYtNTFkY2UyYTIxY2E2IiwidHlwIjoiQmVhcmVyIiwiYXpwIjoiYWNjb3VudCIsImFjciI6IjEiLCJyZXNvdXJjZV9hY2Nlc3MiOnsiYWNjb3VudCI6eyJyb2xlcyI6WyJtYW5hZ2UtYWNjb3VudCIsIm1hbmFnZS1hY2NvdW50LWxpbmtzIiwidmlldy1wcm9maWxlIl19fSwic2NvcGUiOiJwcm9maWxlIGVtYWlsIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJjbGllbnRIb3N0IjoiMTcyLjE3LjAuMSIsImNsaWVudElkIjoiYWNjb3VudCIsInByZWZlcnJlZF91c2VybmFtZSI6InNlcnZpY2UtYWNjb3VudC1hY2NvdW50IiwiY2xpZW50QWRkcmVzcyI6IjE3Mi4xNy4wLjEifQ.OoSm7YQksfB42Ll0QIe3eOMYFpb_AUQQOJj34sigyRJ953GyqeEEm9vjjeV2HuFecfmFeNX4IHwPHPMPoN4MovsHPvajK9AEiFnBmrLo6f3pzk25FTellRM3er_v_9vH0PVm22vg9YrR2fGCvCsJ0w9ULmjnO2jtR7XMaFw0Kw65RrCSaF1HDy1V-_QRAtRQNYBbkEeDomQ5fLei3eKHT498FBACbnsnxi0MvDtlUg-5ttf6r-Okb29JkNnMmYukgTNHQJOuQh4Htvkr_L0HQj3HoVt8mFFA3oSbe7m4IDydFbGBO5ZuooseRDFPMFU7UzMfQjy-pTO8r1C1Yu0fQw",
  "expires_in": 3600,
  "refresh_expires_in": 0,
  "token_type": "Bearer",
  "not-before-policy": 0,
  "scope": "profile email"
}
like image 37
Cesar Celis Avatar answered Dec 26 '22 21:12

Cesar Celis