[英]Keyclok Rest API using node.js
我试图为 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);
您应该首先以管理员身份获取访问令牌,然后使用该令牌创建用户,
curl --location --request POST 'http://localhost:8080/auth/admin/realms/appsdeveloperblog/users' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJFY2dlX3Y0c09fZUZ0TnhqYWJjT19QLTBhQ3p6S2VfMW02OU5mRjlBc1VzIn0.eyJleHAiOjE1OTI1Njc4OTEsImlhdCI6MTU5MjU2NzgzMSwianRpIjoiNjJiMWRlODEtNTBhMS00NzA2LWFmN2MtYzhhZTc1YTg1OTJhIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgwL2F1dGgvcmVhbG1zL21hc3RlciIsInN1YiI6IjNmYjc1YTM4LTA4NjctNGZlYi04ZTBlLWYzMTkxZTZlODZlMSIsInR5cCI6IkJlYXJlciIsImF6cCI6ImFkbWluLWNsaSIsInNlc3Npb25fc3RhdGUiOiJhMDMwNGNiMS0xMzViLTQzODItYjYwMi0xNjNmNzgzYWNlN2IiLCJhY3IiOiIxIiwic2NvcGUiOiJlbWFpbCBwcm9maWxlIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhZG1pbjIifQ.G9-OiyrGWk8cY4S3Ho255Y_euk_gTKDgYmGmU8RPBSeBNtFb_A68tuPFJxFKbzhZ1lipKJCXQsHbStoihvXAmmRsKzud5hDIvvnrD7CcVxAIpbd2wV5i6mB2wVLocV0_FCrE0-DNi_GPAKnazjFiVu3TxxM2L8Zsw7PHN9sb8Ux_lRvAFyNY5bT7NTbmEmt6LsO2An7iTZdBLScK9Lk9ZW8_0WG4eLMy9fatrpVV3MXhINW56gZD8WsWISY0m-cbIftDreZ1f2lzIjMGfkDrgCjy-VZjeIpbmffN-YGrUVywziymBRwA7FFLAxcf6jS5548HVxxKeMPIvNEfDG7eWw' \
--data-raw '{"firstName":"Sergey","lastName":"Kargopolov", "email":"test@test.com", "enabled":"true", "username":"app-user"}'
来源: https : //www.appsdeveloperblog.com/keycloak-rest-api-create-a-new-user/
大家好,我已经以另一种形式重新编写了代码,这对我有用,我可以创建用户
function CreateKCUser(accessToken) {
return axios({
method: 'post',
url,
data: {
username,
email,
enabled
},
headers: {
'Authorization': `Bearer ${accessToken}`
}
})
.then(function (response) {
console.log("User created!!");
})
.catch(function (error) {
console.log("Error User exist!!!");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.