Forum Discussion

Nconforti's avatar
Nconforti
Mentor
5 years ago

roles_to_add/roles_to_remove not working

Hi everyone,

I'm developing a python script and using the Khoros python module. Through this module I'm sending a PUT request to the community to either add or remove roles from specific users. My request looks like:

comm_url = 'https://community.<company>.com/api/2.0/users/6/' #+ user_id
payload = {}
payload={"data":{"type":"user","roles_to_add": "Customer"}}
headers = {"Content-Type":"application/json"}

res = khoros.api.put_request_with_retries(url=comm_url,json_payload=payload,khoros_object=community_name)

At first I thought my python or something was wrong (still could be the case), but if I change "roles_to_add" to "biography", the biography gets updated correctly. But anytime I use roles_to_add or roles_to_remove, I get a 200 response back, but can't see any changes. The audit log also shows no changes.

I'm on Revision: 20.3 (20.3-release r2005190939)
Commit Id: be1c5d989e418530a2eadae02e4bc26cf353f913

Does anyone else have this trouble? Should I use another method to update the roles for a user via a PUT request?

  • ShalinA's avatar
    ShalinA
    5 years ago

    Hi Nconforti,

    I just noticed one issue with the data you are providing in the api request body 

    any roles that you want to add or remove, can you please put them in a list even if it is just one role or more than one? If you still see the problem, I will do further investigation.

    I.E 

    {"data":{"type":"user","roles_to_add":["t:Customer"]}}
  • SuzieH's avatar
    SuzieH
    Khoros Alumni (Retired)

    Nconforti I'm checking with the Engineering team that introduced support for these fields in PUT requests. I hope to hear back soon.

  • Hi Nconforti ,

    It seems that the API request you are making is having the role name as a value to key roles to add, Did you tried passing the role id as a value?

    Also, you can use the API v1 to update the user's role following is the documentation for the same:

    https://devdocportal.lithium.com/t5/Community-API-v1-Reference/bd-p/restv1docs?section=commv1&leaf-id=Role.users.add#Role.users.add

    Please Give Kudos if you find this post useful and also mark this as Accepted Solution if this is the required solution.

    Regards

    Tarun Kumar

    • Nconforti's avatar
      Nconforti
      Mentor

      Hi Tarun I also tried using the id, but it also didn't work. 

      payload={"data":{"type":"user","roles_to_add": "t:Customer"}}
  • ShalinA's avatar
    ShalinA
    Khoros Alumni (Retired)

    Hi Nconforti ,

    I believe you are calling this API to Exasol production communtiy.
    I just checked the community and found that user with id 6 and login fortnico is already assigned the role Customer on top category level. So, when you call the update user API, it is trying to re-assign the same role to the same user.

    Can you please confirm that you are making this API calls against prod or stage community? Also, Can you make sure that you are making this calls as an authorized user so that you have proper permissions to update the role information of the user with ID 6?

  • Hi ShalinA - I removed that role from the user. This is on our production instance and I'm authenticating with a session key using my admin credentials.

    I also tried using CURL with this command:

    curl -X PUT https://community.<company>.com/api/2.0/users/6 -H 'content-type: application/json' -H 'li-api-session-key:<session key>' -d '{"data":{"type":"user","roles_to_add":"t:Customer"}}'

    I also get a success response:

    {"status":"success","message":"","http_code":200,"data":{...}

    Using the same call with the V1 API worked as a workaround, but the v2 API still isn't working

    • ShalinA's avatar
      ShalinA
      Khoros Alumni (Retired)

      Hi Nconforti,

      I just noticed one issue with the data you are providing in the api request body 

      any roles that you want to add or remove, can you please put them in a list even if it is just one role or more than one? If you still see the problem, I will do further investigation.

      I.E 

      {"data":{"type":"user","roles_to_add":["t:Customer"]}}
      • SuzieH's avatar
        SuzieH
        Khoros Alumni (Retired)

        Nconforti I see where you were tripped up by our documentation. I didn't realize that those reference pages (which get generated automatically) do not indicate that this needed to be a string array. I'll be working to get this remedied with our Engineering team, and I'll also be providing examples for adding/removing roles to users in our API Guides.

  • ShalinA SuzieH Yes - wrapping it with brackets works!

    {"data":{"type":"user","roles_to_add":["t:Customer"]}}'

    This was a successful request! 

    If you could expand the documentation or add an example, that would be great for the future!