Skip to content

Instantly share code, notes, and snippets.

@jbdammeier
Created September 26, 2017 01:17
Show Gist options
  • Select an option

  • Save jbdammeier/2ac38e607e78b437218075612422b178 to your computer and use it in GitHub Desktop.

Select an option

Save jbdammeier/2ac38e607e78b437218075612422b178 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 Pyhton 3.6 to make Canvas API call
#
# Author: jdammeier
#
# Created: 9/24/2017
# Copyright: (c) jdammeier 2017
# Licence: <your licence>
#-------------------------------------------------------------------------------
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