search

Home  >  Q&A  >  body text

Always use 401 res in OpenWeather() - react-open-weather library

<p>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 <code>useOpenWeather()</code> always returns <code>401</code> 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. </p> <blockquote> <p>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> </blockquote> <p><strong>My Code</strong></p> <pre class="brush:php;toolbar:false;">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 ( <div> <ReactWeather isLoading={isLoading} errorMessage={errorMessage} data={data} lang="en" locationLabel="Munich" unitsLabels={{ temperature: 'C', windSpeed: 'Km/h' }} showForecast /> </div> ) } export default Weather</pre> <p>I tried reinstalling the React-Open-Weather library, but the problem is still not resolved. </p> <p>The URL I use to confirm my api key: http://api.openweathermap.org/data/2.5/forecast?id=524901&appid=dc5807427c5379fdd34b63326ad4eb54</p>
P粉556159786P粉556159786523 days ago541

reply all(1)I'll reply

  • P粉598140294

    P粉5981402942023-08-19 00:44:13

    According to https://openweathermap.org/api/one-call-api, onecall returns 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 running weather instead of onecall:

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

    And with your parameters, it displays the data correctly.

    reply
    0
  • Cancelreply