AnsweredAssumed Answered

An actual WORKING Canvas Data script in Python

Question asked by David Passey on Jun 21, 2017
Latest reply on Jun 22, 2017 by Stefanie Sanders

Does anyone have an ACTUAL WORKING Python script for accessing the Canvas Data API?  It shouldn't be this hard, but for some reason I cannot get past the 'Invalid api key' error message when I try to run this piece of code below.  I have taken out my key and secret (for obvious reasons).  It's pretty straightforward but for some reason it just won't work.  This is being used on Python 3.X:

import requests
from datetime import datetime
import base64
import hashlib
import hmac

apiKey = ''
apiSecret = ''
apiHost = 'portal.inshosteddata.com'
apiContentType = 'application/json'
apiMethod = 'GET'
apiPath = '/api/account/self/file/latest'
apiTime = datetime.utcnow().strftime('%a, %d %b %y %H:%M:%S GMT')

apiKey = bytes(apiKey,'utf-8')
apiSecret = bytes(apiSecret,'utf-8')

msgList = []
msgList.append(apiMethod)
msgList.append(apiHost)
msgList.append(apiContentType)
msgList.append('')
msgList.append(apiPath)
msgList.append('')
msgList.append(apiTime)
msgList.append(apiSecret)

str = "".join("%s\n" % k for k in msgList).strip()

sig = base64.b64encode(hmac.new(key=apiSecret,msg=bytes(str,'utf-8'),digestmod=hashlib.sha256).digest())
sig = sig.decode('utf-8')

headers = {}
headers['Authorization'] = 'HMACAuth {}:{}'.format(apiKey,sig)
headers['Date'] = apiTime

r = requests.get('https://' + apiHost + apiPath,headers=headers)
print(r.content)

Outcomes