problems with doing an API call
Question:
I want to do an API call using the Forecast air pollution data from this website
https://openweathermap.org/api/air-pollution
The API call of the website state that it is these:
http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat={lat}&lon={lon}&appid={API key}
Now I have a dataframe in pandas which has the longitude and latitude of 180 cities around the world.
To collect the forecast data I gave this
lon= df.Longitude
lat= df.Latitude
appid= 'b0gs3g26768234d11ss6jh722ff100r8e'
url = 'http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat={lat}&lon={lon}&appid={b0gs3g26768234d11ss6jh722ff100r8e}'
r= requests.get(url)
r
r.text
But it always say invalid API key and my API key is activated. I don’t know what I doing wrong. Could someone please help me
Answers:
You are using Python’s f-string in a bad way. The url should be constructed as follows:
url = f"http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat={lat}&lon={lon}&appid={appid}"
Note the f
before the first quote sign and variables names inside the curly braces – {appid}
instead of {<your-app-id>}
.
Also, lat
and lon
should be single values of latitude and longitude, not DataFrame’s columns (e.g lat = 0.0
, lon = 0.0
). You should iterate over the dataframe and send a request for each city (each lat
and lon
values pair).
To be sure that your key is active, you have to take a look at the keys list here (logged in) and check if the status of your key is Active
.
Additional to first answer, your API Key is not activated yet. I tried your key with proper url and params but it didn’t work. For API Weather it takes few hours to activation starts. I had the same issue before.
I want to do an API call using the Forecast air pollution data from this website
https://openweathermap.org/api/air-pollution
The API call of the website state that it is these:
http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat={lat}&lon={lon}&appid={API key}
Now I have a dataframe in pandas which has the longitude and latitude of 180 cities around the world.
To collect the forecast data I gave this
lon= df.Longitude
lat= df.Latitude
appid= 'b0gs3g26768234d11ss6jh722ff100r8e'
url = 'http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat={lat}&lon={lon}&appid={b0gs3g26768234d11ss6jh722ff100r8e}'
r= requests.get(url)
r
r.text
But it always say invalid API key and my API key is activated. I don’t know what I doing wrong. Could someone please help me
You are using Python’s f-string in a bad way. The url should be constructed as follows:
url = f"http://api.openweathermap.org/data/2.5/air_pollution/forecast?lat={lat}&lon={lon}&appid={appid}"
Note the f
before the first quote sign and variables names inside the curly braces – {appid}
instead of {<your-app-id>}
.
Also, lat
and lon
should be single values of latitude and longitude, not DataFrame’s columns (e.g lat = 0.0
, lon = 0.0
). You should iterate over the dataframe and send a request for each city (each lat
and lon
values pair).
To be sure that your key is active, you have to take a look at the keys list here (logged in) and check if the status of your key is Active
.
Additional to first answer, your API Key is not activated yet. I tried your key with proper url and params but it didn’t work. For API Weather it takes few hours to activation starts. I had the same issue before.