Has anyone had trouble accessing courses in the Canvas LMS via the requests library? I’ve been using python and the canvasapi library to interact with Canvas courses programmatically. I find however that I am unable to use the requests library to interact with Canvas. For example I saw a posting A Simple Python GET Script on the Instructure forum.
I typed in the code below.
import config #-- my api key is in this file.
import requests
url = config.API_URL
headers = {'Authorization' : 'Bearer ' + config.API_KEY}
r = requests.get(url, headers = headers)
print(r.status_code)
print(r.text)
The status code returned is 200. The last print statement did fill my terminal window with unformatted HTML, but it was the code for our Canvas login page, not the Canvas URL I was trying to reach. A rep from my school’s IT department suggested pasting my key directly into the code instead of getting it from an external file, which made no difference. Specifying a different Canvas URL made no difference. This is only one concrete example of the requests library not working for Canvas.
I should quickly add that this API key, tucked quietly away in my config.py, works fine for interacting with Canvas using the canvasapi library.
I am running Python 3.10 under Windows 10. I have co-workers at my school using Python 3.10 and 3.11 under Windows 10 who also cannot get the code to work.
When I opened a ticket with my school’s IT department. I was told the code worked for them using python 3.8 under Ubuntu. And that I must have damaged or mishandled my key somehow. But he could never explain how I could damage my key in such a way that the key fails for the requests library but works fine for canvasapi. I was finally told I should just rewrite the endpoints of the canvasapi to include the areas I’m interested in. I don’t know much about APIs in general so this suggestion struck me as difficult, dangerous and ill-advised.
As a stop gap I’m using the selenium library to send a python controlled browser to snoop around for me in areas not supported by the API. Of course I have to manually sign into Canvas using two factor authentication, even though my regular browser session has already been authenticated and the program is already using my API key to interact with Canvas via the API. For the task at hand this is only a little kludgy since I’m only interested in one course at a time. I’m assuming if I needed to test for something in several hundred courses at once the requests library might run faster. Is this correct?
I did post this question to an Instructure forum, where it was promptly deleted.
Please let me know if I’ve overlooked anything, or if there's anything else I should try.
Brian R. Russell Online Learning Production Specialist / Academic Excellence UW Continuum College
UW Tower, Box 359491 4333 Brooklyn Ave NE, Seattle, WA 98195-9480
I contacted my school's IT department. I tried different Canvas URLs. I was expecting to be able to get the html code for any Canvas page I had the authority to view. But it seems like Canvas is not acknowledging my API key in this context.