Using OpenWeatherMap API gives 401 error

hky404 picture hky404 · Oct 13, 2015 · Viewed 11.5k times · Source

I'm trying to get the weather data for London in JSON but I am getting HTTPError: HTTP Error 401: Unauthorized. How do I get the API working?

import urllib2
url = "http://api.openweathermap.org/data/2.5/forecast/daily?q=London&cnt=10&mode=json&units=metric"
response = urllib2.urlopen(url).read()

Answer

davidism picture davidism · Oct 13, 2015

The docs open by telling you that you need to register for an API key first.

To access the API you need to sign up for an API key

Since your url doesn't contain a key, the site tells you you're not authorized. Follow the instructions to get a key, then add it to the query parameters.

http://api.openweathermap.org/data/2.5/forecast/daily?APPID=12345&q=...