我正在使用CKAN 2.2版,並試圖自動創建數據集和上載資源。我似乎無法使用python 請求庫創建數據集。我收到400錯誤代碼。代碼:使用CKAN API和Python請求庫創建CKAN數據集
import requests, json
dataset_dict = {
'name': 'testdataset',
'notes': 'A long description of my dataset',
}
d_url = 'https://mywebsite.ca/api/action/package_create'
auth = {'Authorization': 'myKeyHere'}
f = [('upload', file('PathToMyFile'))]
r = requests.post(d_url, data=dataset_dict, headers=auth)
奇怪的是我是能夠創造新的資源,並使用Python 請求庫上傳文件。該代碼是基於this documentation.代碼:
import requests, json
res_dict = {
'package_id':'testpackage',
'name': 'testresource',
'description': 'A long description of my resource!',
'format':'CSV'
}
res_url = 'https://mywebsite.ca/api/action/resource_create'
auth = {'Authorization': 'myKey'}
f = [('upload', file('pathToMyFile'))]
r = requests.post(res_url, data=res_dict, headers=auth, files=f)
我也能創建使用CKAN文檔中的方法的數據集使用內置的Python庫。文檔:CKAN 2.2
代碼:
#!/usr/bin/env python
import urllib2
import urllib
import json
import pprint
# Put the details of the dataset we're going to create into a dict.
dataset_dict = {
'name': 'test1',
'notes': 'A long description of my dataset',
}
# Use the json module to dump the dictionary to a string for posting.
data_string = urllib.quote(json.dumps(dataset_dict))
# We'll use the package_create function to create a new dataset.
request = urllib2.Request('https://myserver.ca/api/action/package_create')
# Creating a dataset requires an authorization header.
request.add_header('Authorization', 'myKey')
# Make the HTTP request.
response = urllib2.urlopen(request, data_string)
assert response.code == 200
# Use the json module to load CKAN's response into a dictionary.
response_dict = json.loads(response.read())
assert response_dict['success'] is True
# package_create returns the created package as its result.
created_package = response_dict['result']
pprint.pprint(created_package)
,我真的不知道爲什麼我創建的數據集的方法是行不通的。 package_create和resource_create函數的文檔非常相似,我希望能夠使用相同的技術。我寧願使用請求包來處理與CKAN的所有交易。有沒有人能夠成功地創建一個包含請求庫的數據集?
任何幫助,非常感謝。
感謝您發佈此。我永遠不會知道。耶穌 – Jesus