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
JavaScript
x
6
1
import requests
2
3
APIKEY='XXXXXXXXXXXX' # Replaced with provided api key
4
r = requests.get('https://api.speciesplus.net/api/v1/taxon_concepts.xml?name=Mammalia&X-Authentication-Token={APIKEY}')
5
r
6
Advertisement
Answer
As @jonrsharpe said in comment:
JavaScript
1
2
1
"Headers and query parameters aren't the same thing..."
2
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.
JavaScript
1
19
19
1
import requests
2
3
APIKEY = 'XXXXXXXXXXXX'
4
5
headers = {
6
"X-Authentication-Token": APIKEY,
7
}
8
9
payload = {
10
"name": "Mammalia",
11
}
12
13
url = "https://api.speciesplus.net/api/v1/taxon_concepts.xml"
14
15
response = requests.get(url, params=payload, headers=headers)
16
17
print(response.status_code)
18
print(response.text)
19
EDIT:
If you skip .xml
in URL then you get data as JSON and it can be more useful
JavaScript
1
12
12
1
url = "https://api.speciesplus.net/api/v1/taxon_concepts"
2
3
response = requests.get(url, params=payload, headers=headers)
4
5
print(response.status_code)
6
print(response.text)
7
8
data = response.json()
9
10
for item in data:
11
print(item['citation'])
12