Сбой проверки сертификата Python3 - PullRequest
0 голосов
/ 12 марта 2020

Я пытаюсь запустить пример кода, где я получаю список имен онтологий с веб-сайта, и я получаю эту ошибку. Я не совсем уверен, что происходит и что я должен сделать, чтобы решить эту проблему. Любая помощь будет принята с благодарностью!

Это код, который я пытаюсь запустить:

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)

Это сообщение об ошибке, которое я получаю:

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
...