Using Authentication for a Student Account

Jump to solution
BRANDTEGGERS
Community Member

Hello,

I'm a student, and I'm making an application that restricts myself from doing certain things if I have missing assignments. However, I cannot get the authentication to work properly. I'm using an integration token from my user profile.

This is my code (Python): https://pastebin.com/D672jT0U

I have tried many different things to try and fix this, but it doesn't seem to work. The response is always "{'status': 'unauthenticated', 'errors': [{'message': 'user authorization required'}]}"

Labels (3)
0 Likes
1 Solution
chriscas
Community Coach
Community Coach

Hi @BRANDTEGGERS,

I'm going to guess authentication is failing because you're doing token.read() twice.  The first call would read the entire file, presumably containing your API token.  The second time you call that, there's nothing more to read, so it will essentially return nothing (and thus your headers would have no token).  What you probably need to do is something more like:

import requests
tokenfile = open("./canvas.token", "r")
token = tokenfile.read()
if token == "":
	print("No authentication found.")
	quit()
requestUrl = "https://iuniversityprep.instructure.com/api/v1/courses"
headers = {
	"Authorization": "Bearer " + token
}
print(requests.get(requestUrl, headers=headers).json())

This was the first thing that stuck out to me, so it's possible there are other issues going on, but I think this should perhaps point you in the right direction.

-Chris

View solution in original post