2017-03-07 30 views
3

私が働いている組織はCanvas LMSを使用し始めています。プラットフォームのデータを引き出し、データの洞察を助けるために私は担当しました。Pythonを使用してCanvas Data REST APIを使用するにはどうすればよいですか?

それはキャンバスLMSは、Data APIを提供していますが、私は、使用するPythonラッパーをfiding苦労したことは素晴らしいことです。私はここの公式のスタックの一部であるPythonを使ってそれと対話したいと思っていました。

私は公式ドキュメントは(https://portal.inshosteddata.com/docs/api)であることがわかっているが、私は実際に認証方法を使用していないことだし、それは、サンプル・コードを持つことがそんなに簡単になります。

だから、どのように私はキャンバスLMSデータAPIと対話するために私のpythonコードを起動する必要がありますか?

答えて

4

ここに行きます!

は、私は最終的にそれは彼らのウェブサイト(https://community.canvaslms.com/thread/7423)を介しキャンバスコミュニティの助けを借りて行われました。私はStackOverflowが答えを見つけるのが簡単だと信じているので、ここで質問と回答を投稿しています。

これは他の人にとって役に立ちます。

#!/usr/bin/python 

#imports 
import datetime 
import requests 
import hashlib 
import hmac 
import base64 
import json 


#Get the current time, printed in the right format 
def nowAsStr(): 
    currentTime = datetime.datetime.utcnow() 
    prettyTime = currentTime.strftime('%a, %d %b %Y %H:%M:%S GMT') 
    return prettyTime 

#Set up the request pieces 
apiKey = 'your_key' 
apiSecret = 'your_secret' 
method = 'GET' 
host = 'api.inshosteddata.com' 
path = '/api/account/self/dump' 
timestamp = nowAsStr() 

requestParts = [ 
    method, 
    host, 
    '', #content Type Header 
    '', #content MD5 Header 
    path, 
    '', #alpha-sorted Query Params 
    timestamp, 
    apiSecret 
] 

#Build the request 
requestMessage = '\n'.join(requestParts) 
print (requestMessage.__repr__()) 
hmacObject = hmac.new(apiSecret, '', hashlib.sha256) 
hmacObject.update(requestMessage) 
hmac_digest = hmacObject.digest() 
sig = base64.b64encode(hmac_digest) 
headerDict = { 
    'Authorization' : 'HMACAuth ' + apiKey + ':' + sig, 
    'Date' : timestamp 
} 

#Submit the request/get a response 
uri = "https://"+host+path 
print (uri) 
print (headerDict) 
response = requests.request(method='GET', url=uri, headers=headerDict, stream=True) 

#Check to make sure the request was ok 
if(response.status_code != 200): 
    print ('Request response went bad. Got back a ', response.status_code, ' code, meaning the request was ', response.reason) 
else: 
    #Use the downloaded data 
    jsonData = response.json() 
    print json.dumps(jsonData, indent=4) 
関連する問題