You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyOWM调用OpenWeatherMap每日预报API时提示无效API密钥问题排查

问题分析与解决方案

看起来你遇到的问题核心不是API Key无效——毕竟实时天气查询能正常返回结果,说明你的Key是有效的。问题出在OpenWeatherMap免费账号的权限限制上:

  • 免费层级(Free Tier)并不支持daily类型的预报接口,你误以为免费版支持每日预报,但实际上免费版仅提供5天每3小时一次的天气预报(forecast5类型)。而daily预报属于付费订阅层级(比如Starter或更高)的功能,这也是为什么你调用该接口时会触发权限相关的错误,甚至平台界面里每日预报功能也无法正常工作。

快速修复方案

把代码中的daily替换为免费版支持的forecast5,然后调整后续的预报判断逻辑,示例如下:

from pyowm import OWM
from pyowm.utils import config
from pyowm.utils import timestamps

owm = OWM('c548bc34f606696689b7c67ce8cbdbc7')
mgr = owm.weather_manager()
# 实时天气部分保持不变
observation = mgr.weather_at_place('Leiria, PT')
w = observation.weather
print(w.detailed_status)
print(w.wind())
print(w.humidity)
print(w.temperature('celsius'))
print(w.rain)
print(w.heat_index)
print(w.clouds)

# 修改这里:用免费版支持的forecast5
forecast = mgr.forecast_at_place('Leiria, PT', 'forecast5')
# 判断明天是否晴朗:forecast5是每3小时的数据,所以需要指定明天的某个时间点
answer = forecast.will_be_clear_at(timestamps.tomorrow())
print(answer)

额外验证步骤

  1. 登录OpenWeatherMap账号,进入「My API Keys」页面,确认你的订阅层级是Free Tier,查看该层级的可用接口列表,会发现确实没有daily forecast的权限。
  2. 如果确实需要每日预报功能,你需要升级到对应的付费订阅层级,之后才能正常调用daily类型的预报接口。

内容的提问来源于stack exchange,提问作者André Clérigo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 11:59:11