i have a html page with javascript where i want to auto-login the user. I have the following code:
var url = "http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/token";
const response = await fetch(url, {
mode: 'no-cors',
method: "POST",
body: JSON.stringify({
"client_id":"myclientid",
"username":"admin",
"password":"123",
"grant_type":"password"
}),
headers:{
//"Content-type":"application/x-www-form-urlencoded",
"Content-Type": "application/json"
}
})
On the keycloak server i added Web Origins '*'. I get the following error :
POST http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/token 400 (Bad Request)
I dont know why it is not working. When i use the terminal it works fine:
curl -i -d "client_id=myclientid" -d "username=admin" -d "password=123" -d "grant_type=password" http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/token
(keycloak version 4.8.3)
Update
const response = await fetch(url, {
method: "POST",
body: 'client_id=myclientid&password=123&username=admin&grant_type=password',
headers:{
"Content-type":"application/x-www-form-urlencoded"
}
})
and i get the following response:
curl
in your terminal example? If so,-d
sends application/x-www-form-urlencoded, not JSON. Does your API accept JSON input …? – 04FShttp://localhost:8180/
, then of course you need to enable CORS. (If this endpoint is supposed to be called from the client side to begin with - if you have to actually put credentials into client-side code(?), then probably rather not.) – 04FSlocalhost:8080
, so that you are not dealing with any additional CORS issues in this test scenario.) – 04FS