Warm tip: This article is reproduced from serverfault.com, please click

json-使用API​​的授权错误401

(json - Authorization error 401 using API)

发布于 2020-11-28 23:30:00

这是我第一次在Python中使用API​​。我想在网站http://data.bioontology.org上进行查询不确定必须使用什么API_KEY。另外,我在该网站上确实有一个帐户,但不必登录即可获取查询结果。有人可以帮我吗?

import urllib.request, urllib.error, urllib.parse
import json
import os
from pprint import pprint

REST_URL = "http://data.bioontology.org"
API_KEY = ""

def get_json(url):
    opener = urllib.request.build_opener()
    opener.addheaders = [('Authorization', 'apikey token=' + API_KEY)]
    return json.loads(opener.open(url).read())

def print_annotations(annotations, get_class=True):
    for result in annotations:
        class_details = result["annotatedClass"]
        if get_class:
            try:
                class_details = get_json(result["annotatedClass"]["links"]["self"])
            except urllib.error.HTTPError:
                print(f"Error retrieving {result['annotatedClass']['@id']}")
                continue

        print("Annotation details")
        for annotation in result["annotations"]:
            print("\tfrom: " + str(annotation["from"]))
            print("\tto: " + str(annotation["to"]))
            print("\tmatch type: " + annotation["matchType"])

text_to_annotate = "peanut, butter, pita, bread, home"

# Annotate using the provided text
annotations = get_json(REST_URL + "/annotator?text=" + urllib.parse.quote(text_to_annotate))

# Print out annotation details
print_annotations(annotations)

# Annotate with hierarchy information
annotations = get_json(REST_URL + "/annotator?max_level=3&text=" + urllib.parse.quote(text_to_annotate))
print_annotations(annotations)

# Annotate with prefLabel, synonym, definition returned
annotations = get_json(REST_URL + "/annotator?include=prefLabel,synonym,definition&text=" + urllib.parse.quote(text_to_annotate))
print_annotations(annotations, False)

这是错误输出:

HTTPError                                 Traceback (most recent call last)
<ipython-input-15-996831b96ba6> in <module>()
     52 
     53 # Annotate using the provided text
---> 54 annotations = get_json(REST_URL + "/annotator?text=" + urllib.parse.quote(text_to_annotate))
     55 
     56 # Print out annotation details

5 frames
/usr/lib/python3.6/urllib/request.py in http_error_default(self, req, fp, code, msg, hdrs)
    648 class HTTPDefaultErrorHandler(BaseHandler):
    649     def http_error_default(self, req, fp, code, msg, hdrs):
--> 650         raise HTTPError(req.full_url, code, msg, hdrs, fp)
    651 
    652 class HTTPRedirectHandler(BaseHandler):

HTTPError: HTTP Error 401: Unauthorized
Questioner
Ribhu Nirek
Viewed
0
Jonathan Leon 2020-11-30 09:50:25

你需要有一个帐户才能请求你的api密钥。以下摘录来自此页面:https : //www.bioontology.org/wiki/BioPortal_Help

使用BioPortal API进行编程有关如何使用BioPortal REST API来访问信息的文档,请参见:

http://data.bioontology.org/documentation

获取API密钥使用BioPortal REST API需要API密钥。

要检索你的API密钥,请在BioPortal网站上登录你的帐户。如果你没有帐户,则需要创建一个帐户。

你的API密钥将以纯文本形式显示在你的帐户页面上。