0

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
j. doe
  • 163
  • 3
  • 11
  • Does this answer your question? [Python 3 urllib ignore SSL certificate verification](https://stackoverflow.com/questions/36600583/python-3-urllib-ignore-ssl-certificate-verification) – Trapli Mar 12 '20 at 06:08
  • As per answered in the linked question, use the ssl library to tell not to check certificates or switch to requests and use `verify=False`. – Trapli Mar 12 '20 at 06:10
  • @Trapli Thank you for your reply! I looked at the linked question and added `import ssl ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE` before the opener declaration and I am still getting the same error. – j. doe Mar 14 '20 at 01:15
  • update your opener too: `opener = urllib.request.build_opener(urllib.request.HTTPSHandler(context=ctx))` – Trapli Mar 14 '20 at 20:38
  • Hi @Trapli! I updated my opener but this time I am getting an http error. I updated my original code with the new code! Thanks for your help! – j. doe Mar 14 '20 at 21:08
  • Http 401 means that your credentials aren't ok. Check your login data. – Trapli Mar 15 '20 at 08:16

0 Answers0