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)
Solved! Go to Solution.
Well, apparently I answered my own question! In tinkering with the code and reading over the instructions yet again, I was able to get this example working. I hope this helps out somebody else!
import requests
from datetime import datetime
import base64
import hashlib
import hmac
from json import loads
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')
msgList = []
msgList.append(apiMethod)
msgList.append(apiHost)
msgList.append(apiContentType)
msgList.append('')
msgList.append(apiPath)
msgList.append('')
msgList.append(apiTime)
msgList.append(apiSecret)
msgStr = bytes("".join("%s\n" % k for k in msgList).strip(),'utf-8')
sig = base64.b64encode(hmac.new(key=bytes(apiSecret,'utf-8'),msg=msgStr,digestmod=hashlib.sha256).digest())
sig = sig.decode('utf-8')
headers = {}
headers['Authorization'] = 'HMACAuth {}:{}'.format(apiKey,sig)
headers['Date'] = apiTime
headers['Content-type'] = apiContentType
r = requests.get('https://' + apiHost + apiPath,headers=headers)
if r.status_code == 200:
resp = loads(r.text)
print(resp['dumpId'])
Well, apparently I answered my own question! In tinkering with the code and reading over the instructions yet again, I was able to get this example working. I hope this helps out somebody else!
import requests
from datetime import datetime
import base64
import hashlib
import hmac
from json import loads
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')
msgList = []
msgList.append(apiMethod)
msgList.append(apiHost)
msgList.append(apiContentType)
msgList.append('')
msgList.append(apiPath)
msgList.append('')
msgList.append(apiTime)
msgList.append(apiSecret)
msgStr = bytes("".join("%s\n" % k for k in msgList).strip(),'utf-8')
sig = base64.b64encode(hmac.new(key=bytes(apiSecret,'utf-8'),msg=msgStr,digestmod=hashlib.sha256).digest())
sig = sig.decode('utf-8')
headers = {}
headers['Authorization'] = 'HMACAuth {}:{}'.format(apiKey,sig)
headers['Date'] = apiTime
headers['Content-type'] = apiContentType
r = requests.get('https://' + apiHost + apiPath,headers=headers)
if r.status_code == 200:
resp = loads(r.text)
print(resp['dumpId'])