始终使用401 res在OpenWeather()中 - react-open-weather库
P粉556159786
P粉556159786 2023-08-18 09:24:49
0
1
453
<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>
P粉556159786
P粉556159786

全部回复(1)
P粉598140294

根据https://openweathermap.org/api/one-call-apionecall返回所有可能的功能:

  • 当前天气
  • 1小时内的分钟级预报
  • 48小时内的每小时预报
  • 7天的每日预报
  • 国家天气警报
  • 过去5天的历史天气数据

一些功能,如预报,对于免费账户是不允许的。 通过运行weather而不是onecall,您只能获取当前天气:

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

并且使用您的参数,它会正确显示数据。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!