csparpa / pyowm

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

Invalid API key - due to urllib.error.HTTPError: HTTP Error 401: Unauthorized #237

Closed kubow closed 6 years ago

kubow commented 6 years ago

tried both on Windows 10 (python 2.7/3.6.4) and fedora linux (python 2.7/3.6.4)

import pyowm
owm = pyowm.OWM('###')
owm.daily_forecast('Prague,cz')

returns:

Traceback (most recent call last): File "...\lib\site-packages\pyowm\commons\weather_client.py", line 55, in _lookup_cache_or_invoke_API response = urlopen(API_full_url, None, timeout) File "...\lib\urllib\request.py", line 223, in urlopen return opener.open(url, data, timeout) File "...\lib\urllib\request.py", line 532, in open response = meth(req, response) File ...\lib\urllib\request.py", line 642, in http_response 'http', request, response, code, msg, hdrs) File "...\lib\urllib\request.py", line 570, in error return self._call_chain(args) File "...\lib\urllib\request.py", line 504, in _call_chain result = func(args) File "...\lib\urllib\request.py", line 650, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 401: Unauthorized

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "...\lib\site-packages\pyowm\webapi25\owm25.py", line 609, in daily_forecast json_data = self._api.call_API(DAILY_FORECAST_URL, params) File "...\lib\site-packages\pyowm\commons\weather_client.py", line 94, in call_API return self._lookup_cache_or_invoke_API(self._cache, url, timeout) File "...\lib\site-packages\pyowm\commons\weather_client.py", line 58, in _lookup_cache_or_invoke_API raise unauthorized_error.UnauthorizedError('Invalid API key') pyowm.exceptions.unauthorized_error.UnauthorizedError: Your API subscription level does not allow to >perform this operation Reason: Invalid API key

looks like the urllib is not working for me, anyone else having this troubles? Is it possible to implement requests instead?

if I type https://api.openweathermap.org/data/2.5/weather?q=Prague, cz&APPID=### it returns desired json content... so I am sure the api_key is good

the pyowm version should be the latest: pip show pyowm

Name: pyowm Version: 2.8.0 Summary: A Python wrapper around the OpenWeatherMap web API Home-page: http://github.com/csparpa/pyowm

as in http error https://github.com/csparpa/pyowm/issues/102

csparpa commented 6 years ago

Hi @kubow thanks for reporting this

This is likely an issue on the API side (it already happened in the past), I will investigate it as soon as I can.

FYI you can install the development branch of PyOWM that introduces requestsas HTTP client (instead of urllib). That might fix your issue... can you give it a try? You can install with:

git clone https://github.com/csparpa/pyowm.git -b develop
python setup py install  # run from inside the PyOWM root folder
hildogjr commented 6 years ago

Tested the Git code on Ubuntu Python3 (don't needed requests installations) -> Ok!