始终使用401 res在OpenWeather()中 - react-open-weather库
<p>我想在我的React应用程序中添加天气小部件,我尝试使用React-Open-Weather库。但是获取数据的函数<code>useOpenWeather()</code>总是返回<code>401</code>,我通过将它放在react-open-weather文档中提供的URL中确认了我的api-key是有效的。但我不知道出了什么问题。</p>
<blockquote>
<p>错误:GET http://api.openweathermap.org/data/2.5/onecall?appid=dc5807427c5379fdd34b63326ad4eb54&lang=en&units=metric&lat=48.137154&lon=11.576124 401 (未经授权)</p>
</blockquote>
<p><strong>我的代码</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>我尝试重新安装React-Open-Weather库,但问题仍未解决。</p>
<p>我用来确认我的api密钥的URL:http://api.openweathermap.org/data/2.5/forecast?id=524901&appid=dc5807427c5379fdd34b63326ad4eb54</p>