csparpa / pyowm

A Python wrapper around the OpenWeatherMap web API
https://pyowm.readthedocs.io
MIT License
789 stars 171 forks source link

The API worhs for weather_at_place() but not for forecast_at_place() #316

Closed EightFawn closed 4 years ago

EightFawn commented 4 years ago

The API works with:

owm = pyowm.OWM(owm_token)
mgr = owm.weather_manager()
obs = mgr.weather_at_place('Roma')

But if I use:

daily_forecast = mgr.forecast_at_place('Roma', 'daily').forecast

It give me this error:

raise exceptions.UnauthorizedError('Invalid API Key provided')
pyowm.commons.exceptions.UnauthorizedError: Invalid API Key provided

With only the weather_at_place() code it works but if I add at the same program the forecast_at_place() it gives me this error.

csparpa commented 4 years ago

@EightFawn still not working?

I've tried your exact code and it works on my side - I have a free API key Might have been a temporary flicker of the OWM API

Please let me know!

Karan-Ghatt commented 4 years ago

Hi, I seem to be facing a similar issues, I'm trying to run the following code:

from pyowm import OWM

owm = OWM('key')  

mgr = owm.weather_manager()
observation = mgr.weather_at_place('London,GB')
w = observation.weather
print(w)

I keep hitting the same error:

raise exceptions.UnauthorizedError('Invalid API Key provided')
pyowm.commons.exceptions.UnauthorizedError: Invalid API Key provided

I am using a valid key, so I'm not too sure whats going on. Any insight would be great!

csparpa commented 4 years ago

Could you pls paste here the output of a print of your OWM object instance? That is to say:

print(owm)

thanks

EightFawn commented 4 years ago

It gives me the same error, the output of the print of owm is:

<pyowm.owm.OWM - API key=************************b81f5079, subscription type=free, PyOWM version=(3, 0, 0)>

csparpa commented 4 years ago

IoI looks like there is no reason why it shouldn't work!

Short of ideas.. Have you tried creating a new free API key and use that one instead?

EightFawn commented 4 years ago

I create a new API key but I have to wait that openweathermap verify the key

EightFawn commented 4 years ago

It gives the same error...

EightFawn commented 4 years ago

I tried to remove the part of the weather and leave only forecast but it still not working

csparpa commented 4 years ago

@EightFawn I've tested your exact code again on my side and it works like a charm

Now, I think here the issue is not with PyOWM but with the API key itself In your very first code snippet you said that querying for observed weather worked but querying for daily forecasted weather didn't: both queries were working fine on my side.

So here my conjecture is: as my free API key was issued years ago and it seems that it calls OWM API endpoints that are now unaccessible to newer API keys.

I've checked out on the OWM API website: it seems that as of today, only calls to the "OneCall" endpoint are enabled for free API key - this might explain why calls work on my side (because I have a kind of "legacy" API key) and don't work on yours.

This behaviour was completely unexpeted to me :-S

Luckily, you'll hopefylly be able to retrieve both observed and daily forecasted data using the OneCall methods

EightFawn commented 4 years ago

Ok I try thanks

EightFawn commented 4 years ago

It gives me an error when I try to find the geographic coordinates by the name, I used the same instructions of the wiki , the error is:

city = list_of_locations[0][0] TypeError: 'Location' object is not subscriptable

EightFawn commented 4 years ago

I have to open another issue for this?

csparpa commented 4 years ago

No, I'll check this out thanks

geofbaum commented 4 years ago

@EightFawn try the following to see if you get valid lat and lon values.

city = list_of_locations[0]
print(city.lat, city.lon)

@csparpa It appears that the example in the docs has an extra [0]:

from pyowm.owm import OWM
owm = OWM('your-api-key')
reg = owm.city_id_registry()
list_of_locations = reg.locations_for('moscow', country='RU')
moscow = list_of_locations[0][0]
lat = moscow.lat   # 55.75222
lon = moscow.lon   # 37.615555

As in moscow = list_of_locations[0][0] should be moscow = list_of_locations[0]. Either that or the code is returning something that isn't expected from the Locations class.

EightFawn commented 4 years ago

Yes, now it works, thanks

csparpa commented 4 years ago

Ok, I'll patch the docs then

raghuvarranvh commented 3 years ago

@EightFawn I've tested your exact code again on my side and it works like a charm

Now, I think here the issue is not with PyOWM but with the API key itself In your very first code snippet you said that querying for observed weather worked but querying for daily forecasted weather didn't: both queries were working fine on my side.

So here my conjecture is: as my free API key was issued years ago and it seems that it calls OWM API endpoints that are now unaccessible to newer API keys.

I've checked out on the OWM API website: it seems that as of today, only calls to the "OneCall" endpoint are enabled for free API key - this might explain why calls work on my side (because I have a kind of "legacy" API key) and don't work on yours.

This behaviour was completely unexpeted to me :-S

Luckily, you'll hopefylly be able to retrieve both observed and daily forecasted data using the OneCall methods

Works like a charm.. Was stumped for a while. thanks

Polly333 commented 3 years ago
reg = owm.city_id_registry()
list_of_locations = reg.locations_for('Mumbai', country='IN')
Mumbai = list_of_locations[0]
lat = Mumbai.lat
lon = Mumbai.lon

one_call = mgr.one_call(lat, lon)

One_call = one_call.forecast_daily[0].temperature('celsius')

print(One_call) 

Does it return temperature for the next day?

csparpa commented 3 years ago

@Polly333 yep

ividal commented 2 years ago

If you need to access forecaster's will_have_storm(), will_have_hurricane(), etc., which one_call doesn't give access to (as far as I could find) but don't care about the frequency, "3h" instead of "daily" does the trick.

Weirdly enough, this won't work even with a new API key:

forecast = mgr.forecast_at_place('Roma', 'daily').forecast # Invalid API key

but this does work: forecast = mgr.forecast_at_place('Roma', '3h').forecast

Not sure if this is a problem in pyowm or on the openweathermaps side, but this workaround helped me (pyowm==3.2.0).

csparpa commented 2 years ago

@ividal opening up a new issue to investigate what you report It looks like the old API endpoints started to behave differently (with 3h) than how they did at the times (with daily) Thanks for sharing this!