In my HTML page with JavaScript, I am trying to implement auto-login functionality for the user. Below is the code I have written:
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/json"
}
})
In the Keycloak server, I have added Web Origins '*'. However, I am encountering the following error:
POST http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/token 400 (Bad Request)
I am unsure why it is not working as expected. Interestingly, when I use the terminal, it functions correctly:
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"
}
})
Upon making the above update, I received the following response: