Home > other >  problems with doing an API call
problems with doing an API call

Time:11-01

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

CodePudding user response:

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 {b0gs3g26768234d11ss6jh722ff100r8e}.

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).

CodePudding user response:

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.

  • Related