【发布时间】:2021-02-09 18:39:59
【问题描述】:
我试图为 keyclok 应用程序编写一个 crud 函数。我能够获取令牌、使用令牌并获取用户列表但不能创建用户 如果有人可以帮助我,那对我很有帮助
创建用户列表(无效)
function CreateKCUser(accessToken) {
let url = `http://localhost:8080/auth/admin/realms/msportal/users`;
return axios_instance.post(url,
{
headers: {
"content-type": "application/json",
"authorization": `Bearer ${accessToken}`
},
data: {
"firstName": "First",
"lastName": "Last",
"email":"test@test.com",
"enabled":"true",
"username":"TEST-USER"
}}).then(function (response) {
console.log('User Created');
})
.catch(function (error) {
console.log('Error on Creation');
});
}
获取用户列表(工作)
function GetKCUser(email, accessToken) {
let url = `http://localhost:8080/auth/admin/realms/msportal/users?email=${email}`;
return axios_instance.get(url,
{
headers: {
"content-type": "application/json",
"accept": "application/json",
"authorization": `Bearer ${accessToken}`
}
});
}
功能(令牌,让用户工作,除了用户创建)
http.createServer(function Test() {
getAccessToken().then(function (response) {
console.log('access_token=' + response.data.access_token);
GetKCUser("shafeenes@gmail.com", response.data.access_token).then((resp) => {
console.log(resp.data);
});
CreateKCUser(response.data.access_token).then((resp) => {
console.log(resp);
});
}).catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});;
}).listen(8084);
【问题讨论】:
-
恕我直言,您正在重新发明轮子。我会使用github.com/keycloak/keycloak-nodejs-admin-client - 它实现了大多数 Keycloak Admin REST api 调用。当然,使用过的用户必须具有适当的用户/客户端权限。您可能拥有查看用户的权限,但没有创建用户的权限。
标签: node.js api rest axios keycloak