Re: [Canvas] User Authorization Error when Trying to List Courses [Python]

1,424 views
Skip to first unread message

Chris Herdt

unread,
Nov 7, 2013, 12:15:55 AM11/7/13
to canvas-l...@googlegroups.com
Hi Simon,

For requests that require authentication, you'll need to send the access token as a header (rather than as a get request parameter), e.g.:
headers = {"Authorization": "Bearer " + my_access_token}
courses = requests.get("https://canvas.instructure.com/api/v1/courses", headers = headers)




On Wed, Nov 6, 2013 at 5:59 PM, <simo...@gmail.com> wrote:
Hello,

I am trying to learn the API using Canvas API Lessons. I am having trouble with Lesson 6, the instructions are as followed:

Sweet, let's get moving now. For this next endpoint you'll need to be enrolled in at least one course. If you're not, go create one and you should be auto-enrolled as an instructor.

I want you to get the list of courses you're enrolled in and tell me the id of the first course returned by the API call.

I am doing this lesson in Python using Requests for the API calls, when i run my program in the command line, the error says: 

(u'status', u'unauthenticated')
(u'errors', {u'message': u'user authorization required'})

I am confused because I thought I was authorized with my access token, the Canvas API lessons even say don't worry about OAuth yet because everything is done with the Access Tokens for now.

Here is my Python code for this lesson:

import requests
import json

def main():
   
    # GET request for user information
    # user_info = requests.get("https://canvas.instructure.com/api/v1/users/self/profile?access_token=<MY_ACCESS_TOKEN_HERE>") #
   
    # Storing user information in dictionary called data
    # user_info_dictionary = json.loads(user_info.text)

    courses = requests.get("https://canvas.instructure.com/api/v1/courses")
    courses_dictionary = json.loads(courses.text)

    '''
    for u in user_info_dictionary:
        print(u, user_info_dictionary[u])

    print("\n")
    '''

    for u in courses_dictionary:
        print(u, courses_dictionary[u])

main()



How can I retrieve my list of courses?

--
 
---
You received this message because you are subscribed to the Google Groups "Canvas LMS Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to canvas-lms-use...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Chris Herdt
Web Applications Developer
267-603-1066 (home)
734-754-3585 (cell)
http://osric.com/chris/

simo...@gmail.com

unread,
Nov 8, 2013, 2:28:52 PM11/8/13
to canvas-l...@googlegroups.com
Thank you! :)
Reply all
Reply to author
Forward
0 new messages