Skip to content
Advertisement

Authorization error 401 using API – Python

This is my first time using an API in Python. I want to give a query on the website http://data.bioontology.org. Not sure what the API_KEY has to be. Also, I do have an account on this website but I do not have to log in to get a query result. Can someone please help me out?

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)

Here’s the error output:

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

Advertisement

Answer

You need to have an account to request your api key. Excerpt below is from this page: https://www.bioontology.org/wiki/BioPortal_Help

Programming with the BioPortal API Documentation about how to use the BioPortal REST API to access information is available here:

http://data.bioontology.org/documentation

Getting an API key Use of the BioPortal REST API requires an API key.

To retrieve your API key, log into your account on the BioPortal website. If you don’t have an account, you’ll need to create one.

Your API key will be listed in plain text on your account page.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement