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.
<SPAN class="comment token"># check for numeric string, in which case this a Canvas user_id</SPAN><BR /> <SPAN class="keyword token">if</SPAN> person_id<SPAN class="punctuation token">.</SPAN>isdigit<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN><BR /> info<SPAN class="operator token">=</SPAN>user_info<SPAN class="punctuation token">(</SPAN>person_id<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">elif</SPAN> person_id<SPAN class="punctuation token">.</SPAN>count<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'-'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># a sis_integration_id</SPAN><BR /> info<SPAN class="operator token">=</SPAN>user_info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'sis_integration_id:'</SPAN><SPAN class="operator token">+</SPAN>person_id<SPAN class="punctuation token">)</SPAN><BR /> integration_id<SPAN class="operator token">=</SPAN>person_id <SPAN class="comment token"># since we have the ID, save it for later</SPAN><BR /> <SPAN class="keyword token">elif</SPAN> person_id<SPAN class="punctuation token">.</SPAN>find<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'@'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">></SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="comment token"># if an e-mail address/login ID</SPAN><BR /> info<SPAN class="operator token">=</SPAN>user_info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'sis_login_id:'</SPAN><SPAN class="operator token">+</SPAN>person_id<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN><BR /> <SPAN class="comment token"># assume it is a local university ID</SPAN><BR /> info<SPAN class="operator token">=</SPAN>user_info<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'sis_user_id:'</SPAN><SPAN class="operator token">+</SPAN>person_id<SPAN class="punctuation token">)</SPAN><BR /><BR /> <SPAN class="comment token"># extract Canvas user-id from the user's info:</SPAN><BR /> user_id<SPAN class="operator token">=</SPAN>info<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'id'</SPAN><SPAN class="punctuation token">]</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"sortable name={}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>info<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'sortable_name'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Canvas user_id={}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>user_id<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /><BR /> <SPAN class="comment token"># try to get the user's integration_id via their profile</SPAN><BR /> user_profile<SPAN class="operator token">=</SPAN>user_profile_info<SPAN class="punctuation token">(</SPAN>user_id<SPAN class="punctuation token">)</SPAN><BR /> integration_id<SPAN class="operator token">=</SPAN>user_profile<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'integration_id'</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">)</SPAN><BR /> login_id<SPAN class="operator token">=</SPAN>user_profile<SPAN class="punctuation token">.</SPAN>get<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'login_id'</SPAN><SPAN class="punctuation token">,</SPAN> None<SPAN class="punctuation token">)</SPAN><BR /> <SPAN class="keyword token">if</SPAN> login_id<SPAN class="punctuation token">:</SPAN><BR /> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"login_id={}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>login_id<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><BR /><BR /><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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 .