Python使用Ckan API查询用户的私有数据集

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coffee_cream/article/details/52064101
#-*- coding: utf-8 -*-
__author__ = 'LILI'

import requests
import json
import pprint

CKAN_URL = 'http://default.yourckan.com'
UserName = 'lili'
APIkey = 'API Key'

def getUserPrivateDataset(APIkey, UserName, apikey):
    url = '{ckan}/api/3/action/user_show?id={user}&include_datasets=True'.format(
        ckan=CKAN_URL, user=UserName)

    headers = {}
    headers['Content-Type'] = 'application/json'
    headers['X-CKAN-API-Key'] = apikey
    headers['Authorization'] = apikey
    headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'

    r = requests.post(url, headers=headers)

    assert r.status_code == 200
    # Use the json module to load CKAN's response into a dictionary.
    response_dict = json.loads(r.text)

    # Check the contents of the response.
    assert response_dict['success'] is True
    result = response_dict['result']
    return result['datasets']

if __name__ == '__main__':
    result = getUserPrivateDataset(APIkey, UserName, APIkey)
    print(result)

将代码最前面的CKAN_URLUserNameAPIkey分别置换为自己的CKAN网址、用户名和API key。

猜你喜欢

转载自blog.csdn.net/coffee_cream/article/details/52064101