cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ddieckmeyer
Community Participant

Creating a custom role using a Canvas API

Jump to solution

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

Accepted Solutions
stuart_ryan
Community Coach
Community Coach

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

5 Replies
stuart_ryan
Community Coach
Community Coach

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

ddieckmeyer
Community Participant

Hi Stuart, 

Finally able to get back to this. I set up curl in my windows environment and tested your code. It worked just fine. So I went back to my python script and incrementally tested it and it also works. I'm not certain what was different, but all is good now. 

I realized that if a permission is excluded in the "Create a New Role" api the base role permissions are applied. So I'll need to specify ALL permissions that I want to change. Otherwise the Python script works. 

Thanks for taking the time to help! 

Dave

Hi Dave,

No worries at all, tis the busy season! Great to hear that it seems to be working now, let's put it down to a glitch in the matrix :smileygrin:.

That is also good to know regarding needing to specify all the permissions. Something I will have to keep in mind in future (appreciate your sharing it).

Always happy to help out, share some knowledge and work through a conundrum (haha 'tis what I enjoy).

Have a great weekend.

Stuart

ddieckmeyer
Community Participant

One last comment and special NOTE:

The permissions are divided into two groups, account level and course level. If the permission you're interested in is an account level permission you won't be able to apply it to a 'course level' base role (such as Student, TA or Observer). 

Cheers!

   Dave

OOh also very good to note! Thank you for sharing your insights, it is great and very helpful for others that may run into this in future (haha myself included!).

Cheers,
Stuart