英文:
SvelteKit and keycloak-js, code_to_token_error and invalid_client_credentials error
问题
我正在使用 keycloak-js 来保护我的 SvelteKit UI 应用程序。
<script lang="ts">
import { onMount } from "svelte";
import Keycloak from "keycloak-js";
import type { KeycloakInitOptions } from "keycloak-js";
onMount(() => {
let instance = {
url: "http://localhost:8080",
realm: "my-realm",
clientId: "my-client"
};
let keycloak = new Keycloak(instance);
let initOptions: KeycloakInitOptions = { onLoad: "login-required"
};
keycloak.init(initOptions).then(function (authenticated) {
console.info("authorized");
}).catch(function () {
alert("unauthorized");
});
}
);
</script>
我在我的 Docker 中部署的 Keycloak 上收到了以下错误。
WARN [org.keycloak.events] (executor-thread-69) type=CODE_TO_TOKEN_ERROR, realmId=..., clientId=my-client, userId=null, ipAddress=..., error=invalid_client_credentials, grant_type=authorization_code
然而,从 Postman 中检索令牌是有效的。
英文:
I'm using keycloak-js to secure my sveltekit UI app.
<script lang="ts">
import { onMount } from "svelte";
import Keycloak from "keycloak-js";
import type { KeycloakInitOptions } from "keycloak-js";
onMount( () => {
let instance = {
url: "http://localhost:8080",
realm: "my-realm",
clientId: "my-client"
};
let keycloak = new Keycloak(instance);
let initOptions: KeycloakInitOptions = { onLoad: "login-required"
};
keycloak.init(initOptions).then(function (authenticated) {
console.info("authorized);
}).catch(function () {
alert("unauthorized");
});
}
);
</script>
I'm receiving this error on my keycloak deployed in docker.
WARN [org.keycloak.events] (executor-thread-69) type=CODE_TO_TOKEN_ERROR, realmId=..., clientId=my-client, userId=null, ipAddress=..., error=invalid_client_credentials, grant_type=authorization_code
Retrieving the token from postman works though.
答案1
得分: 2
根据您的错误消息:
WARN [org.keycloak.events] (executor-thread-69) type=CODE_TO_TOKEN_ERROR, realmId=..., clientId=my-client, userId=null, ipAddress=..., error=invalid_client_credentials, grant_type=authorization_code
看起来Keycloak正在期望一个client secret
,根据您的配置:
let instance = {
url: "http://localhost:8080",
realm: "my-realm",
clientId: "my-client"
};
推测客户端my-client
是一个保密客户端。然而,由于您正在使用javascript
适配器,您应该实际上创建一个公开的客户端,而不是保密的客户端。
所以,请将客户端my-client
的access-type
从confidential更改为public。
英文:
Based on your error message:
WARN [org.keycloak.events] (executor-thread-69) type=CODE_TO_TOKEN_ERROR, realmId=..., clientId=my-client, userId=null, ipAddress=..., error=invalid_client_credentials, grant_type=authorization_code
it looks like Keycloak is expecting a client secret
, which based on your configuration:
let instance = {
url: "http://localhost:8080",
realm: "my-realm",
clientId: "my-client"
};
infers that the client my-client
is a confidential one. However, since you are using the javascript
adapter, you should actually create a public client instead of a confidential one.
So change the access-type
of the client my-client
from confidential to public.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论