Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么会出现401错误;API密钥丢失“;调用OpenWeatherMapAPI时?_Python_Python 3.x_Python Requests_Openweathermap - Fatal编程技术网

Python 为什么会出现401错误;API密钥丢失“;调用OpenWeatherMapAPI时?

Python 为什么会出现401错误;API密钥丢失“;调用OpenWeatherMapAPI时?,python,python-3.x,python-requests,openweathermap,Python,Python 3.x,Python Requests,Openweathermap,我试图用OpenWeatherMap.org制作python天气报告。但是,python不断给我一个关于API键的错误。它说它不见了 import requests r = requests.get("https://api.openweathermap.org/data/2.5/weather?q=London&appid={API key}") print(r.status_code) 输出为401。我试着把我的API密钥放进去,但还是不起作用。而这个API密

我试图用OpenWeatherMap.org制作python天气报告。但是,python不断给我一个关于API键的错误。它说它不见了

import requests

r = requests.get("https://api.openweathermap.org/data/2.5/weather?q=London&appid={API key}")
print(r.status_code)

输出为401。我试着把我的API密钥放进去,但还是不起作用。而这个API密钥是什么?

这个
{API Key}
是您实际的API密钥的占位符,您可以从openweathermap帐户页面获得它(这也意味着您需要先注册一个帐户)。应该是这样的:

appid='12345678901234567890123456789012'
r = requests.get(f'https://api.openweathermap.org/data/2.5/weather?q=London&appid={appid}')
打印将为您提供更多信息:

In [2]: r = requests.get("https://api.openweathermap.org/data/2.5/weather?q=London&appid={API key}")

In [3]: r.status_code
Out[3]: 401

In [6]: r.content
Out[6]: b'{"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}'
位于的页面将解释您需要提供有效的API密钥,并将引导您找到他们的:

关于如何使用API密钥进行API调用的示例
API调用

http://api.openweathermap.org/data/2.5/forecast?id=524901&appid={API
key}
参数

appid
必需
您的唯一API密钥(您总是可以在 您的帐户页(位于下)


好吧,你没有用f字串。因此,您应该将字母
f
放在URL之前。您需要从这里获得自己的API密钥:才能访问他们的API。来自的
{API key}
意味着是一个占位符。您需要将实际的API密钥放入其中(“您可以在您的帐户页面的“API密钥”选项卡下找到它”)。谢谢!如果你把你的答案放在评论里,我就不能给你的答案打分。我把我的评论变成了一个答案。此外,我建议删除指向使用实际API密钥的代码的链接。您不应该公开共享API密钥或令牌。