12

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()
John Slegers
  • 45,213
  • 22
  • 199
  • 169
hky404
  • 1,039
  • 3
  • 17
  • 35

9 Answers9

20

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=...
davidism
  • 121,510
  • 29
  • 395
  • 339
8

Error: Invalid API key. Please see http://openweathermap.org/faq#error401 for more info

API calls responds with 401 error: You can get the error 401 in the following cases:

here are some steps to find problem.

1) Check if API key is activated

some API services provide key information in dashboard whether its activated, expired etc. openWeatherMap don't. to verify whether your key is working 'MAKE API CALL FROM BROWSER' api.openweathermap.org/data/2.5/weather?q=peshawar&appid=API_key

replace API_key with your own key, if you get data successfully then your key is activated otherwise wait for few hours to get key activated.

2) Check .env for typos & syntax

.env is file which is used to hide credentials such as API_KEY in server side code. make sure your .env file variables are using correct syntax which is NAME=VALUE

API_KEY=djgkv43439d90bkckcs

no semicolon, quotes etc

3) Check request URL

check request url where API call will be made , make sure

  • It doesn't have spaces, braces etc
  • correct according to URL encoding
  • correct according to API documentation

4) Debug using dotenv:

to know if you dotenv package is parsing API key correctly use the following code

const result = dotenv.config()

if (result.error) {
  throw result.error
} 
console.log(result.parsed)

this code checks if .env file variables are being parsed, it will print API_KEY value if its been parsed otherwise will print error which occur while parsing.

Hopefully it helps :)

Muhammad Uzair
  • 394
  • 5
  • 9
3

I also faced the same issue, I have just created an account on open weather map and also verified the email, tried to load the api using several different url , but they replied with 401 , api key not found.

Solution: after 1 hour they all started working, so the reason was for activation it took 1 or some more hours.

cigien
  • 57,834
  • 11
  • 73
  • 112
Ajay Raut
  • 69
  • 1
  • 6
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33148033) – isaactfa Nov 14 '22 at 22:30
1

For a graduate i was helping, he had a correct api key and it was active, but the api was incorrectly 401 when no content type was given

it was a simple as adding a Content-Type: application/json, and hey presto the api started working

curl command

curl --location \
  --request GET \
  'https://api.openweathermap.org/data/2.5/forecast?lat=55&lon=-3&appid=xxx' \
  --header 'Content-Type: application/json'
aqm
  • 2,942
  • 23
  • 30
0

The api key not set in your url ! before all you must register in https://openweathermap.org/ then get api key in your pesrsonal account after that do it like this: http://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY_HERE&units=metric

replace you apikey code with {YOUR_API_KEY_HERE} then run your app.

Amin1994
  • 11
  • 1
0

After registering, you need to verify email.

cigien
  • 57,834
  • 11
  • 73
  • 112
modeverv
  • 54
  • 4
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30993454) – Simas Joneliunas Feb 09 '22 at 04:46
0

On the website API docs, it tells you to connect to the API using this URL: "https://api.openweathermap.org/data/3.0/onecall?lat=$latitude&lon=$longitude&appid=$apiKey"

However, after you've verified your email, you will receive another email informing you of your API key as well as an example of the API call URL: "https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longtitude&APPID=$apiKey"

I am on the free subscription and have tried the 1st URL multiple times but access was always denied. It only started working after I used the 2nd URL (Provided to me in the 2nd email).

Roy
  • 137
  • 1
  • 1
  • 8
0

Sometimes it also happens because of wrong naming of parameter in url.

url = f"https://api.openweathermap.org/data/2.5/forecast?lat={latitude}&lon={longitude}&appid={api_key}"

In my case I was giving onecall in place "forecast". This typeof silly mistakes also happened sometime. Thanks.

Gkr
  • 41
  • 3
0

You must have a valid API key by registering with openweathermap and confirming your email AND make sure you are compliant with their latest API.

A valid API key via registering is applied by appending the key to your URL with &appid=<yourkey>

Make sure the REST API URL is compliant with their current API specification found at https://openweathermap.org/current#geo.

They have made refactoring recently and removed some parts such as "daily" from the API so also results in an error 401 if added so remove it.

So change your url to : url = "http://api.openweathermap.org/data/2.5/forecast?q=London&cnt=10&mode=json&units=metric&appid=<yourkey>