我正在使用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