Skip to content

Instantly share code, notes, and snippets.

@jbdammeier
Last active September 26, 2017 01:20
Show Gist options
  • Save jbdammeier/32ef361c7279e0a3e65b1884f9ddebb1 to your computer and use it in GitHub Desktop.
Save jbdammeier/32ef361c7279e0a3e65b1884f9ddebb1 to your computer and use it in GitHub Desktop.
Demo of setting up basic API call
#-------------------------------------------------------------------------------
# Name: canvas_api_demo.py
#
# Purpose: Sample Program to use Python 3.6 to make Canvas API calls
#
# Author: jdammeier
#-------------------------------------------------------------------------------
import requests, json
from pprint import pprint
from urllib.request import Request, urlopen
#Enter Canvas APi token to set up API Call
token = <'yourtokenhere'>
request_headers={'Authorization': token}
#set data for call to canvas and for export
#prepare url using info in user and name variables. Consult the canva API guide (https://canvas.instructure.com/doc/api/)
#to return other objects or information
#replace <self> in a url with <user_id> or <sis_user_id:<sis_user_id>> if calling for someone else, provided you have correct permission
#Everything after ? in the url specifies objects to be included according to documentation
#url = 'https://guerincatholic.instructure.com/api/v1/users/self/courses.json?include[]=total_scores&enrollment_state=active'
#this url gets basic canvas profile information.
url = 'https://guerincatholic.instructure.com/api/v1/users/self.json'
#info for each api call
q = Request(url)
q.add_header('Authorization', 'Bearer ' + token)
u = urlopen(q)
#make the call and retrieve data
data = json.loads(u.read().decode())
#print returned data that can be assigned to any variable or returned form any function
pprint(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment