始终使用401 res在OpenWeather()中 - react-open-weather库
P粉556159786
P粉556159786 2023-08-18 09:24:49
[React讨论组]
<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&amp;lang=en&amp;units=metric&amp;lat=48.137154&amp;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) =&gt; { const { data, isLoading, errorMessage } = useOpenWeather({ key: 'dc5807427c5379fdd34b63326ad4eb54', lat: '48.137154', lon: '11.576124', lang: 'en', unit: 'metric', // values are (metric, standard, imperial) }); return ( &lt;div&gt; &lt;ReactWeather isLoading={isLoading} errorMessage={errorMessage} data={data} lang="en" locationLabel="Munich" unitsLabels={{ temperature: 'C', windSpeed: 'Km/h' }} showForecast /&gt; &lt;/div&gt; ) } export default Weather</pre> <p>我尝试重新安装React-Open-Weather库,但问题仍未解决。</p> <p>我用来确认我的api密钥的URL:http://api.openweathermap.org/data/2.5/forecast?id=524901&amp;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}

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号