I am trying to access the cites species api to get information on a input species name.
Reference document: http://api.speciesplus.net/documentation/v1/references.html
I tried to use the api with the provided API Key.
I get error code 401.
Here is the code
import requests APIKEY='XXXXXXXXXXXX' # Replaced with provided api key r = requests.get('https://api.speciesplus.net/api/v1/taxon_concepts.xml?name=Mammalia&X-Authentication-Token={APIKEY}') r
Advertisement
Answer
As @jonrsharpe said in comment:
"Headers and query parameters aren't the same thing..."
You have to set APIKEY
as header
– don’t put it in URL
.
You may also put parameters as dictionary and requests
will append them to URL
– and code will be more readable.
import requests APIKEY = 'XXXXXXXXXXXX' headers = { "X-Authentication-Token": APIKEY, } payload = { "name": "Mammalia", } url = "https://api.speciesplus.net/api/v1/taxon_concepts.xml" response = requests.get(url, params=payload, headers=headers) print(response.status_code) print(response.text)
EDIT:
If you skip .xml
in URL then you get data as JSON and it can be more useful
url = "https://api.speciesplus.net/api/v1/taxon_concepts" response = requests.get(url, params=payload, headers=headers) print(response.status_code) print(response.text) data = response.json() for item in data: print(item['citation'])