Reputation: 23
Hi I am buliding my first web app using javascript and fetching data using API from www.openweathermap.org/ I have used the API key as mentioned in the documentation still it is giving an error of unauthorization. Can there be any other reason for this error while calling a function or so . Thank you in advance.
var APPID = "my_secret_key";
var temp;
var loc;
var icon;
var wind;
var humidity;
var direction;
function updateByZip(zip){
var url = "http://api.openweathermap.org/data/2.5/weather?" +
"zip = " + zip +
"&APPID =" + APPID ;
sendRequest(url);
}
function sendRequest(url){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var data = JSON.parse(xmlhttp.responseText) ;
var weather = {};
weather.wind = data.wind.speed;
weather.direction = data.wind.deg;
weather.loc = data.name;
weather.temp = data.main.temp;
weather.icon = data.weather[0].id;
weather.humidity=data.main.humidity;
update(weather);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
Upvotes: 1
Views: 6818
Reputation: 1
For those who followed the previous answers and are still facing the 401 issue: it seems it is now required to access the the API via HTTPS --- at least that's the case for me. Some older guides and tutorials might continue to use http://
in their code, so you'll have to change it to https://
.
As far as I know, there is no mention of this in OpenWeather's official docs, and they don't include the protocol in their examples too.
Upvotes: 0
Reputation: 494
for future users, as i was having 401 error but solved it differently.
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.
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.
.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
check request url where API call will be made , make sure
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 :)
look for location of .env file in your directory, moving it to root directory might help (suggested in comments)
Upvotes: 0
Reputation: 7211
It's the spaces near the equal signs in your URL. It's likely urlencoding the space and sending your parameter as APPID%20
which is not being recognized as valid.
var url = "http://api.openweathermap.org/data/2.5/weather?" +
"zip=" + zip +
"&APPID=" + APPID;
Upvotes: 3