checkout Using OpenWeatherMap API gives 401 error here you will find some reasons why your apikey is not working. also, it takes 2 hours for key activation.
Answer from Ninad7N on Stack Overflowcheckout Using OpenWeatherMap API gives 401 error here you will find some reasons why your apikey is not working. also, it takes 2 hours for key activation.
Might be a bit late to reply, but the URL you are using is no longer part of the free plan and that's why you get that error. Try using the following URL, it requires the user to input a city name and there is no need of using latitude and longitude.
https://api.openweathermap.org/data/2.5/weather?q=${*cityName*}&appid=${*API_key*}&units=metric
So, if you have a text field that takes the input for a city name, then the above fetch call would work.
Here cityName is the location entered by the user and API_key is the API key you are assigned. By having &units=metric, you will get the temperature in degree Celcius. If you want the temperature in Farenheit, use &units=imperial or if you want the temperature in Kelvin, just get rid of the &units=metric part.
So for example if you paste the following in your browser, you will get the weather in London.
api.openweathermap.org/data/2.5/weather?q=London&APPID=YOUR_API_KEY
Hope my first answer on this platform helps somebody.
Openweathermap APIKey not accepted - Home Assistant Community
OpenWeatherMap invalid API key with newly created key
Invalid API key error for Open Weather ( free ) API error, despite using a valid key
[SOLVED] Can't retrieve OpenWeatherMap data. Invalid API key
Videos
I tried to run a short program from the book Automate The Boring Stuff With Python, and, my result doesn't match the result predicted by the book. Here's the code:
import requests,json,sys
APPID ='7b26c92417fd3678d52eac12dc870222'
if len(sys.argv) < 2:
print('Usage: getOpenWeather.py city_name, 2-letter_country_code')
sys.exit()
location = ''.join(sys.argv[1:])
#Download the JSON data from OpenWeatherMap.org's API.
url = f'https://api.openweathermap.org/data/2.5/forecast/daily?q={location}&cnt=3&APPID={APPID}'
response = requests.get(url)
response.raise_for_status()
#Uncomment to see the raw JSON text:
#print(response.text)
#Load JSON data into a Python variable.
weatherData = json.loads(response.text)
#Print weather descriptions
w = weatherData['list']
print('Current weather in %s:'%(location))
print(w[0]['weather'][0]['main'],'-',w[0]['weather'][0]['description'])
print()
print('Tomorrow:')
print(w[1]['weather'][0]['main'],'-',w[1]['weather'][0]['description'])
print()
print('Day after tomorrow:')
print(w[2]['weather'][0]['main'],'-',w[2]['weather'][0]['description'])The response I got was:
Traceback (most recent call last):
File "C:\Users\Vesna\PycharmProjects\getOpenWeather.py", line 13, in <module>
response.raise_for_status()
File "C:\Users\Vesna\AppData\Roaming\Python\Python39\site-packages\requests\models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.openweathermap.org/data/2.5/forecast/daily?q=Belgrade,Serbia&cnt=3&APPID=7b26c92417fd3678d52eac12dc870222Then I commented out the raise_for_status function, and added a print statement regarding the response object from the API call ( just uncommented it ), and got this instead:
{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}
Traceback (most recent call last):
File "C:\Users\Vesna\PycharmProjects\getOpenWeather.py", line 22, in <module>
w = weatherData['list']
KeyError: 'list'Both messages allude to the same perceived root cause: lack of valid API key. However, this is just not the case. According to the website I used ( open weather ), a few things could cause the given error ( 401 ): invalid API key, not enough time spent after generating your first key, and trying to access premium services. I did get a valid API key, and even generated another one just to test it out ( with no different result ), I waited hours after my first attempt, so the possibility of my key not yet having finished the process of authorization is minimal, and I've checked multiple time which API services I requested, and they were the free ones ( current weather ) ONLY.
I have no idea what could be causing this error, and would immensely appreciate it if anyone helped me out with this, if they spot or know something I don't.
So when I try to add my API key, I get an error message telling me its not valid. I have contacted openweathermap who confirmed it's an active key. I did a search and people have previously suggested to make sure there's no spaces which I have done and I'm still getting the error message. I've tried typing it in too instead of copy and pasting with no joy.
Thanks for any help!
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=...
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:
- You did not specify your API key in API request.
- Your API key is not activated yet. Within the next couple of hours, it will be activated and ready to use.
- You are using wrong API key in API request. Please, check your right API key in personal account.
- You have free subscription and try to get access to our paid services (for example, 16 days/daily forecast API, any historical weather data, Weather maps 2.0, etc). Please, check your tariff in your personal account.
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 :)