I am trying to run a sample code where I retrieve a list of ontology names from a website and I get this error. I'm not really sure what is going on and what I should do to fix this issue. Any help would be greatly appreciated!
This is the code I am trying to run:
import urllib.request, urllib.error, urllib.parse
import json
import ssl
import requests
import os
from pprint import pprint
REST_URL = "http://data.bioontology.org"
API_KEY = ""
def get_json(url):
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx))
opener.addheaders = [('Authorization', 'apikey token=' + API_KEY)]
return json.loads(opener.open(url).read())
# Get the available resources
resources = get_json(REST_URL + "/")
# Get the ontologies from the `ontologies` link
ontologies = get_json(resources["links"]["ontologies"])
# Get the name and ontology id from the returned list
ontology_output = []
for ontology in ontologies:
ontology_output.append(f"{ontology['name']}\n{ontology['@id']}\n")
# Print the first ontology in the list
pprint(ontologies[0])
# Print the names and ids
print("\n\n")
for ont in ontology_output:
print(ont)
This is the error message I am getting:
Traceback (most recent call last):
File "listOnt.py", line 23, in <module>
ontologies = get_json(resources["links"]["ontologies"])
File "listOnt.py", line 17, in get_json
return json.loads(opener.open(url).read())
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 531, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 640, in http_response
response = self.parent.error(
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 569, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 502, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 401: Unauthorized