Description
Describe the bug
File.get_contents() now gets a 401 Unauthorized error.
To Reproduce
from canvasapi import Canvas
canvas = Canvas('url', 'token')
course = canvas.get_course(12345)
file = course.get_file(987654321)
print(file.get_contents())
Expected behavior
Should print the file contents.
Environment information
- Python version: 3.11.5
- CanvasAPI version: 3.3.0
Additional context
The existing code worked without issue last week. First noticed the problem today (April 10, 2025).
My guess is that Canvas API changed how it processes the file download urls in such a way that canvasapi
is now doing it wrong.
The token is valid: all the other commands described work fine.
Workaound
Instead of using .get_contents()
I now use:
import requests
...
requests.get(file.url).text
Which works just file.
The file url
field has an access token as a query parameter, so you don't need an Auth header to access the file using the credentialed URL (hence a plain requests.get
works fine).
My hypothesis is that the canvasapi
requester is always including the auth headers (reasonable), but perhaps there is now an issue when trying to access the credentialed file URL while also passing the auth header?