Yel*_*own 5 javascript keycloak
我有一个带有 javascript 的 html 页面,我想在其中自动登录用户。我有以下代码:
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"
}
})
Run Code Online (Sandbox Code Playgroud)
在 keycloak 服务器上我添加了Web Origins '*'。我收到以下错误:
POST http://localhost:8180/auth/realms/Myrealm/protocol/openid-connect/token 400(错误请求)
我不知道为什么它不起作用。当我使用终端时,它工作正常:
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
Run Code Online (Sandbox Code Playgroud)
(钥匙斗篷版本4.8.3)
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"
}
})
Run Code Online (Sandbox Code Playgroud)
我得到以下回复:
它现在正在工作。问题是发送 JSON 而不是,application/x-www-form-urlencoded并且还获取 ReadableStream 而不是字符串作为响应。现在这是我的代码:
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"
}
});
response.body.getReader().read().then(function (data){
var string = new TextDecoder("utf-8").decode(data.value);
console.log(string);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11147 次 |
| 最近记录: |