我正在尝试将所有用户上传到 keycloak 用户列表的代码,它运行良好。我使用 excel 文件读取然后将每一行推入 keycloak 字段,但我正在考虑同时为每个用户添加角色。当我尝试此代码时,它说角色字段在用户中不存在并给我错误
这是我的代码
从 Excel 中读取并存储在数组中
let path = 'http://localhost:8080/auth/admin/realms/msportal';
let _userTobeCreated = ReadFromExcel('./uploads/employees-roles.xlsx');
function ReadFromExcel(filename) {
let userTobeCreated = []
xlsxFile(filename).then((rows) => {
rows.forEach(row => {
let userObject = {
username: row[0],
lastName: row[1],
firstName: row[2],
email: row[3],
roles: row[4].split(',')
}
userTobeCreated.push(userObject);
});
})
return userTobeCreated;
}
创建函数
function CreateKCBulkUser(user) {
let urlCreateUser = `${path}/users`;
return axios({
method: 'post',
url: urlCreateUser,
data: JSON.stringify(user),
headers: {
'content-type': 'application/json',
'Authorization': `Bearer ${kc_accessToken}`
}
})
.then(function (response) {
console.log("User created!!");
_userlists = response.data;
})
.catch(function (error) {
console.log(error);
});
}
调用函数
_usersToBeCreated.forEach((v)=>{
CreateKCBulkUser(v);
})
excel文件