Creating a custom role using a Canvas API

Jump to solution
ddieckmeyer
Community Novice

I realized that we needed to create a few custom roles in our primary account and decided to give it a try using the API provided in the API documentation. It seemed simple enough, but I'm having trouble getting the permissions to apply based on the rules in the description. I use this site to paste any curl that's offered ( see the curl source at the bottom of the 'Create a new role' section in the api docs ) to get an idea of how I should be formatting my Python code, then I tweak it.

This is the result:

import requests

headers = {
   'Authorization': 'Bearer <token>',
}

files = {
   'label': (None, 'New Role'),

   'base_role_type': (None,'TaEnrollment'),
   'permissions[read_course_content][explicit]': (None, '1'),
   'permissions[read_course_content][enabled]': (None, '1'),
   'permissions[read_course_list][locked]': (None, '1'),
   'permissions[read_question_banks][explicit]': (None, '1'),
   'permissions[read_question_banks][enabled]': (None, '0'),
   'permissions[read_question_banks][locked]': (None, '1'),
}

requests.get('https://<canvas>/api/v1/accounts/:account_id/roles.json', headers=headers, files=files)

After running the script, the role is created but without any of the permissions as written. I added the 'base_role_type' to the mix and it creates it accordingly. What I don't get is when I change the 'None' or '1' to something different as described in the documentation, it doesn't change the permissions of the newly created role. 

Any ideas about what might be going on and how to correct it so I can customize a new role?

Thanks,

      Dave

Labels (1)
1 Solution
stuart_ryan
Community Novice

Hi  @ddieckmeyer ,

I have had a bit of a review of the APIs and tinker, I have come up with the following call which seems to work in my test instance. I am hoping you could give this a go straight from the command line to see if it works. I would like to see if it works directly in CURL first up, before translating to Python (just to make sure it works at each step).

If it works in CURl, then can you give it a try through your Python translation. I check and each of the permissions were set correctly when I ran this in curl, so hopefully we can narrow down the problem for you.

curl 'https://<yourinstance>/api/v1/accounts/<accountID>/roles.json' \
-H "Authorization: Bearer <token>" \
-F 'label=Stuart API Test Role2' \
-F 'permissions[read_course_content][explicit]=1' \
-F 'permissions[read_course_content][enabled]=1' \
-F 'permissions[read_course_list][locked]=1' \
-F 'permissions[read_question_banks][explicit]=1' \
-F 'permissions[read_question_banks][enabled]=0' \
-F 'permissions[read_question_banks][locked]=1' \
-F 'permissions[change_course_state][explicit]=1' \
-F 'permissions[change_course_state][enabled]=1' \
-F 'permissions[change_course_state][locked]=1'

Let me know how you get on!

Cheers,
Stuart

View solution in original post