我正在尝试access_token使用 axios 使用客户端凭据从 keycloak 获取。但是,当我使用 axios 发布请求获取 access_token 时,出现 400 错误。
我没有使用邮递员测试我的客户端凭据,它确实返回了 access__token 但是,在 NextJS 应用程序中使用时,我收到 400 错误:
目前在我的_app.tsx文件中,我有以下方法:
const getToken = () => {
let token: string = undefined;
const realm = process.env.NEXT_PUBLIC_KEYCLOAK_REALM;
const keycloakClientSecret = process.env.NEXT_PUBLIC_KEYCLOAK_BEARER_CLIENT_SECRET;
const kcTokenEndpoint = `http://localhost:8080/auth/realms/${realm}/protocol/openid-connect/token`;
axios({
method: 'POST',
url: kcTokenEndpoint,
data: {
client_id: 'keycloak-token-bearer', // create client in keycloak with same name
client_secret: keycloakClientSecret,
grant_type: 'client_credentials',
},
headers: {
'Content-type': 'application/x-www-form-urlencoded',
},
withCredentials: true,
})
.then(response => {
token = (response as any)?.access_token;
})
.catch(error => {
token = undefined;
});
return token;
};
我看到返回 a400 (Bad Request) error
当我检查响应时,我看到以下内容:
error "invalid_request"
error_description "Missing form parameter: grant_type"