Skip to content

Instantly share code, notes, and snippets.

@howff
Created December 11, 2018 09:44
Show Gist options
  • Save howff/bcb445120016ef93210a4f782a362695 to your computer and use it in GitHub Desktop.
Save howff/bcb445120016ef93210a4f782a362695 to your computer and use it in GitHub Desktop.
Test CKAN api key
This URL works when logged in as an admin:
http://172.16.92.142/api/3/action/package_search?include_private=True&include_drafts=True
This Python script does not work:
#!/usr/bin/env python2
import urllib2
import json
api_key = '1417c21c-7d04-48df-8f1b-8878edbd6153'
url='http://172.16.92.142/api/3/action/package_search?include_private=True&include_drafts=True'
request = urllib2.Request(url)
request.add_header('X-CKAN-API-Key', api_key)
request.add_header('Authorization', api_key)
response_dict = json.loads(urllib2.urlopen(request, '{}').read())
print("Got %d results" % response_dict['result']['count'])
@boykoc
Copy link

boykoc commented Dec 11, 2018

Hey,

Testing this on my end I think I narrowed down the issue.

Try changing line 13 from response_dict = json.loads(urllib2.urlopen(request, '{}').read()) to

response_dict = json.loads(urllib2.urlopen(request).read()) removing the empty dict.

Or, remove the query strings / params from your url and include them here like

response_dict = json.loads(urllib2.urlopen(request, '{"include_private":"true", "include_drafts":"true"}').read())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment