import urllib2
url = "http://api.openweathermap.org/data/2.5/forecast/daily?q=London&cnt=10&mode=json&units=metric"
response = urllib2.urlopen(url).read()
import urllib2
url = "http://api.openweathermap.org/data/2.5/forecast/daily?q=London&cnt=10&mode=json&units=metric"
response = urllib2.urlopen(url).read()
错误: 无效的API密钥。请参阅http://openweathermap.org/faq#error401获取更多信息
API调用响应401错误: 以下情况会导致您收到错误401:
以下是找到问题的一些步骤。
一些API服务在仪表板中提供密钥信息,例如是否已激活、过期等。openWeatherMap没有。
要验证密钥是否起作用,请使用浏览器进行“MAKE API CALL FROM BROWSER”测试
api.openweathermap.org/data/2.5/weather?q=peshawar&appid=API_key
将API_key替换为您自己的密钥,如果成功获取数据,则您的密钥已激活,否则请等待几个小时以激活密钥。
.env是一个文件,用于在服务器端隐藏凭据,例如API_KEY。 确保您的.env文件变量使用正确的语法 NAME=VALUE
API_KEY=djgkv43439d90bkckcs
检查将进行API调用的请求URL, 确保:
为了知道dotenv包是否正确解析了API密钥,请使用以下代码:
const result = dotenv.config()
if (result.error) {
throw result.error
}
console.log(result.parsed)
curl --location \
--request GET \
'https://api.openweathermap.org/data/2.5/forecast?lat=55&lon=-3&appid=xxx' \
--header 'Content-Type: application/json'
--request
和--header
标志是不必要的,也能让事情正常运行。 - undefined注册后,您需要验证电子邮件。
您的URL中未设置API密钥!首先,您必须在https://openweathermap.org/注册,然后在个人帐户中获取API密钥,然后按照以下步骤操作: http://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY_HERE&units=metric
将您的API密钥代码替换为{YOUR_API_KEY_HERE},然后运行您的应用程序。
&appid=<yourkey>
,可以通过注册获得有效的API密钥。url = "http://api.openweathermap.org/data/2.5/forecast?q=London&cnt=10&mode=json&units=metric&appid=<yourkey>