Flexible entry of user identification to a program to use via APIs

maguire
Community Champion
0
899

After the fun yesterday of using the dashboard cards to make the entry of a course ID easier (Using the dashboard information via the API in programs ). I decided to do a similar thing for user IDs, so rather than have to enter a Canvas user_id for each program - why not be more flexible with a "person_id". The result is a program that can take any of several forms of user identification in and get a Canvas user-id for this user. It also shows how to use some of the SIS IDs at Object IDs, SIS IDs, and special IDs - Canvas LMS REST API Documentation.

    # check for numeric string, in which case this a Canvas user_id
if person_id.isdigit():
info=user_info(person_id)
elif person_id.count('-') == 4: # a sis_integration_id
info=user_info('sis_integration_id:'+person_id)
integration_id=person_id # since we have the ID, save it for later
elif person_id.find('@') > 1: # if an e-mail address/login ID
info=user_info('sis_login_id:'+person_id)
else:
# assume it is a local university ID
info=user_info('sis_user_id:'+person_id)

# extract Canvas user-id from the user's info:
user_id=info['id']
print("sortable name={}".format(info['sortable_name']))
print("Canvas user_id={}".format(user_id))

# try to get the user's integration_id via their profile
user_profile=user_profile_info(user_id)
integration_id=user_profile.get('integration_id', None)
login_id=user_profile.get('login_id', None)
if login_id:
print("login_id={}".format(login_id))

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The routines user_info() and user_profile_info() just call the GET /api/v1/users/:id and GET /api/v1/users/:id/profile APIs respectively.

This has some relation to the question I ask in Searching for users and my several iterations of addressing the question.

Also, as noted in my reply to What is the use case for "integration_ids"? in order to get the user's integration_id, i.e., the sis_integration_id) - I had to use the GET /api/v1/courses/:course_id/enrollments API. I'm not completely sure why all of these different IDs for a user are not returned in the User structure returned by GET /api/v1/users/:id .