This one seems like it should be easy, particularly since I'm using Canvas data instead of our SIS data to test it out.
Looking at the API guide, the Canvas Course ID for the path, and User ID for 'enrollment[user_id]' are the only things required. I am also passing a value for 'enrollment[type]': enrollment_type.
Here is what I have:
def enroll(course_id, user_id, role):
api_url = canvas_url + '/v1/courses/' + str(course_id) + '/enrollments'
if role == 'student':
enrollment_type = 'StudentEnrollment'
else:
enrollment_type = 'TeacherEnrollment'
parameters = {
'enrollment[user_id]': str(user_id),
'enrollment[type]': enrollment_type
}
result = requests.post(api_url,headers=header,data=parameters).json()
print(result)
enroll(3270,4325,'student')
When I run this, the result barfs up what appears to be the code for an HTML page. Here is the error I'm getting:
Traceback (most recent call last):
File "enroll_user_in_course.py", line 25, in <module>
enroll(3270, 4325, 'student')
File "enroll_user_in_course.py", line 22, in enroll
result = requests.post(api_url,headers=header,data=parameters).json()
File "models.py", line 917, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: [Errno Expecting value]
Canvas API programming is starting to make me feel mentally deficient! 😵