简体   繁体   中英

Keyclok Rest API using node.js

I was trying to code a crud function for keyclok app. I am able to get token, use token and get user list but not create a user if someone can help me it will be very helpful for me

create user list (not working)

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');
    });
}

get user list(working)

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}`
      }
    });
}

function (token, get users working except user creation)

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);

You should first get an access token as an admin, then use the token to create a user,

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"}'

Source: https://www.appsdeveloperblog.com/keycloak-rest-api-create-a-new-user/

hello guys i have re written the code in another form and this works for me i am able to create 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!!!");
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM