0

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.

Adobe
  • 12,967
  • 10
  • 85
  • 126
  • I'd try with plain `curl` call from the command line first: `curl -s -L "https:///api/v1/" -X GET -H "Authorization: Bearer "` to make sure everything is in place. – Adobe Jul 06 '23 at 05:06
  • @Adobe Thanks for getting back to me. I’m getting a warning that my comment is too long so I’m splitting it into smaller pieces. I’m not sure what running a curl command will prove. I’ve been using my API key quite often when using python and the canvasapi library. I’m assuming that Canvas doesn’t know or care if I’m using python or curl or which library I'm using. It only knows if my requests have the correct syntax and that I’m only attempting to interact with resources that I have permission to access. Let me know if I’m wrong about this. – Brian at Work Jul 07 '23 at 22:17
  • @Adobe If I run the curl command and it succeeds what does that tell me? If the key was invalid or damaged I shouldn’t be able to use the canvasapi library, which I use often. If I run the curl command and it fails what does that tell me? Again the key works for the canvasapi library, but not requests, and in this case not curl either. – Brian at Work Jul 07 '23 at 22:18
  • @Adobe Most employees at our school do not have permissions to install software on our computers. I would have to go to a help desk to get curl installed. I don’t mind doing that but I’m not sure the test you’ve suggested is going to shed any light on my problem. Thank you, Adobe, for taking the time to read and reply to my posting. Please do let me know if you think of anything else I should try, or if there is something I’m missing in your first suggestion. – Brian at Work Jul 07 '23 at 22:19

0 Answers0