始終使用401 res在OpenWeather()中 - react-open-weather庫
P粉556159786
P粉556159786 2023-08-18 09:24:49
0
1
452
<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.1371548.13715448.134148.1370148.134148.48.1448.48. ;/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學習者快速成長!