Always use 401 res in OpenWeather() - react-open-weather library
P粉556159786
P粉556159786 2023-08-18 09:24:49
0
1
406

I want to add weather widget in my React application and I try to use React-Open-Weather library. But the function to get the data useOpenWeather() always returns 401 which I confirmed by putting it in the URL provided in the react-open-weather documentation The api-key is valid. But I don't know what's wrong.

Error: GET http://api.openweathermap.org/data/2.5/onecall?appid=dc5807427c5379fdd34b63326ad4eb54&lang=en&units=metric&lat=48.137154&lon=11.576124 401 (Unauthorized)< ;/p>

My Code

import React from 'react' import ReactWeather, { useOpenWeather } from 'react-open-weather'; const Weather = (props) => { const { data, isLoading, errorMessage } = useOpenWeather({ key: 'dc5807427c5379fdd34b63326ad4eb54', lat: '48.137154', lon: '11.576124', lang: 'en', unit: 'metric', // values are (metric, standard, imperial) }); return ( 
) } export default Weather

I tried reinstalling the React-Open-Weather library, but the problem is still not resolved.

The URL I use to confirm my api key: http://api.openweathermap.org/data/2.5/forecast?id=524901&appid=dc5807427c5379fdd34b63326ad4eb54

P粉556159786
P粉556159786

reply all (1)
P粉598140294

According tohttps://openweathermap.org/api/one-call-api,onecallreturns all possible functions:

  • Current weather
  • Minute-level forecast within 1 hour
  • Hourly forecast for 48 hours
  • Daily forecast for 7 days
  • National Weather Alert
  • Historical weather data for the past 5 days

Some features, such as forecasts, are not allowed for free accounts. You can only get the current weather by runningweatherinstead ofonecall:

https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}

And with your parameters, it displays the data correctly.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!