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
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'])
I have been given an assignment and part of it is to have it return the outcome of my the date my pods were deployed:
Using the age of the deployment return what happened on that day in history using this API: https://api.wikimedia.org/wiki/API_reference/Feed/On_this_day
We are supposed to make a Python microservice and for this part of it the Wikimedia API reference shows this as example Python code:
import datetime
import requests
today = datetime.datetime.now()
date = today.strftime('%m/%d')
url = 'https://api.wikimedia.org/feed/v1/wikipedia/en/onthisday/all/' + date
headers = {
'Authorization': 'Bearer YOUR_ACCESS_TOKEN',
'User-Agent': 'YOUR_APP_NAME (YOUR_EMAIL_OR_CONTACT_PAGE)'
}
response = requests.get(url, headers=headers)
data = response.json()
print(data)
How can I get an authorization or token or whatever it's asking for or what "user-agent" is?
I am using python to send a request to an API in order to pull sensor data.
The first request that I am sending is to gauge the current time of the sensor.
The second request is to gauge the amount of time, since the sensor identified motion.
I am looking to subtract the first request, by the second.
Below is the method of pulling the data I am using, which works fine. I just cant figure out why it doesn't work when I try and subtract them.
Any help would be much appreciated.
url = "IPADDRESS"
payload={}
headers = {
"Authorization":"Password"
}
response1 = requests.request("GET", url, headers=headers, data=payload, verify=False)
print('Current Time: ', response1.text)
url = "IPAddress"
payload={}
headers = {
"Authorization": "Password"
}
response = requests.request("GET", url, headers=headers, data=payload, verify=False) #HTTP request
print('Time since last motion: ', response.text)
updatedtime = response1.text
occupancelevel = response.text
result = updatedtime - occupancelevel
print(result)
Subtraction only works with some datatypes. (Specifically, only objects which have a .__sub__ method can be subtracted.)
I'm not sure what .data is, as per the docs it doesn't exist, but if it does it clearly isn't something subtractable.
If you are getting a simple number back from your endpoint, you can cast it first:
response = float(response1.data) - float(response2.data)
Otherwise you are going to have to dig into what the response type is, and work accordingly. (Hint: if you don't know, it's probably json.)
Incidentally if, as I think, there is no undeclared .data, you want .text or .json() to get at the response.
References
https://docs.python-requests.org/en/latest/api/#requests.Response
I went to the Skyscanner API documentation page, and selected "Python (Requests)" in the language selector. Then I copied the code snippet and changed the inbound and outbound dates to valid dates (i.e., dates later than today). But after running the code, I get blank results. Why is that?
import requests
url = "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0"
payload = "inboundDate=2020-05-20&cabinClass=business&children=0&infants=0&country=US¤cy=USD&locale=en-US&originPlace=SFO-sky&destinationPlace=LHR-sky&outboundDate=2020-05-15&adults=1"
headers = {
'x-rapidapi-host': "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com",
'x-rapidapi-key': "MY_API_KEY", # need to fill up a form to get the Key
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
>>> {}
Did you checked response example of that API?
Most of time when API have to return True then response blank also.
I can't figure out how to call this api correctly using python urllib or requests.
Let me give you the code I have now:
import requests
url = "http://api.cortical.io:80/rest/expressions/similar_terms?retina_name=en_associative&start_index=0&max_results=1&sparsity=1.0&get_fingerprint=false"
params = {"positions":[0,6,7,29]}
headers = { "api-key" : key,
"Content-Type" : "application/json"}
# Make a get request with the parameters.
response = requests.get(url, params=params, headers=headers)
# Print the content of the response
print(response.content)
I've even added in the rest of the parameters to the params variable:
url = 'http://api.cortical.io:80/rest/expressions/similar_terms?'
params = {
"retina_name":"en_associative",
"start_index":0,
"max_results":1,
"sparsity":1.0,
"get_fingerprint":False,
"positions":[0,6,7,29]}
I get this message back:
An internal server error has been logged # Sun Apr 01 00:03:02 UTC
2018
So I'm not sure what I'm doing wrong. You can test out their api here, but even with testing I can't figure it out. If I go out to http://api.cortical.io/, click on the Expression tab, click on the POST /expressions/similar_terms option then paste {"positions":[0,6,7,29]} in the body textbox and hit the button, it'll give you a valid response, so nothing is wrong with their API.
I don't know what I'm doing wrong. can you help me?
The problem is that you're mixing query string parameters and post data in your params dictionary.
Instead, you should use the params parameter for your query string data, and the json parameter (since the content type is json) for your post body data.
When using the json parameter, the Content-Type header is set to 'application/json' by default. Also, when the response is json you can use the .json() method to get a dictionary.
An example,
import requests
url = 'http://api.cortical.io:80/rest/expressions/similar_terms?'
params = {
"retina_name":"en_associative",
"start_index":0,
"max_results":1,
"sparsity":1.0,
"get_fingerprint":False
}
data = {"positions":[0,6,7,29]}
r = requests.post(url, params=params, json=data)
print(r.status_code)
print(r.json())
200
[{'term': 'headphones', 'df': 8.991197733061748e-05, 'score': 4.0, 'pos_types': ['NOUN'], 'fingerprint': {'positions': []}}]
So, I can't speak to why there's a server error in a third-party API, but I followed your suggestion to try using the API UI directly, and noticed you're using a totally different endpoint than the one you're trying to call in your code. In your code you GET from http://api.cortical.io:80/rest/expressions/similar_terms but in the UI you POST to http://api.cortical.io/rest/expressions/similar_terms/bulk. It's apples and oranges.
Calling the endpoint you mention in the UI call works for me, using the following variation on your code, which requires using requests.post, and as was also pointed out by t.m. adam, the json parameter for the payload, which also needs to be wrapped in a list:
import requests
url = "http://api.cortical.io/rest/expressions/similar_terms/bulk?retina_name=en_associative&start_index=0&max_results=1&sparsity=1.0&get_fingerprint=false"
params = [{"positions":[0,6,7,29]}]
headers = { "api-key" : key,
"Content-Type" : "application/json"}
# Make a get request with the parameters.
response = requests.post(url, json=params, headers=headers)
# Print the content of the response
print(response.content)
Gives:
b'[[{"term":"headphones","df":8.991197733061748E-5,"score":4.0,"pos_types":["NOUN"],"fingerprint":{"positions":[]}}]]'