Showing results for 
Show  only  | Search instead for 
Did you mean: 
New Member

Authentication Required

As the instructor for this particular course, when I typed a url like the following in the browser after I logon to Canvas: 

I received a reply like the following:

"membership_type":"StudentEnrollment","readable_name":"Course Home","asset_class_name":null,
"icon":"icon-home"}}, ...

However, when I use python code to call the same url using the token I generated from my account. Instead I got the following response. 

<Response [401]> {     "status": "unauthenticated",     "errors": [         {             "message": "user authorization required"         }     ] }

Would anyone advise how to avoid getting this error code and be able to access the response in json file?



Labels (1)
3 Replies
Community Member

Hi Paul,

This is not a typical Canvas API call, and that is probably why you get that unauthorized error. You may consider use this endpoint (Users - Canvas LMS REST API Documentation ) instead.



Thanks Mu He, I understood that was not a typical API, but since I can still call it on a browser directly, I thought

"that endpoint should be published or publicly accessible"?

Further, I tried the following, even though I am the instructor of the course, I got the same error:

GET /api/v1/users/:user_id/page_views

On the other hand, I encounter no such errors using other APIs that does not pertain to a particular user_id, such as

the content of the forums, etc. of this course, e.g., 

GET /api/v1/courses/:course_id/users/:user_id

Perhaps there are "2 layers of authentication" that is needed, and we can do it through python code. For example,

using the generated token that I can use to do  GET /api/v1/courses/:course_id/users/:user_id, then a second 

layer that would allow one to do GET /api/v1/users/:user_id/page_views? 

Perhaps with the same second layer authentication, one can then do GET /courses/1111/users/2222/usage.json ?

Community Member


There are endpoints that are used by Canvas and are only available through the web interface and not through the API. One of them is what I use to obtain the Access Report for each student in a class, which is the one that you're showing in your code. Another is one that I use to create a rubric from a spreadsheet. Another was when I assigned peer reviews to everyone in a group (that might have had an API call, but the internal call was faster and easier).

For this kind of call, you need to be inside a browser. They do not recognize the access token used for the API, they use the csrf token cookie instead. One possibility is to run a headless browser that logs you into Canvas and then makes the non-API calls for you. While possible, it's definitely an advanced technique normally reserved for when all else fails and you really, desperately need something. I recently figured out how to do this with Arc (Instructure's video platform) -- at least I run a headless browser long enough to get an access token that I can then use.

The endpoint that Mu gave you is for page views. It requires admin access to view since it is not limited to a single course. Your teacher access does not have sufficient permissions to access that call, just like you can't access the page views within Canvas. The API uses the same permissions as the web interface, so you won't be able to access something through the API if you don't have the permissions to do so in the web version. Other calls are ones that you have permissions to access as a teacher and so those work.