The docs open by telling you that you need to register for an API key first.
打开文档时会告诉您,您需要首先注册一个API密钥。
To access the API you need to sign up for an API key
Since your url doesn't contain a key, the site tells you you're not authorized. Follow the instructions to get a key, then add it to the query parameters.
由于您的url不包含密钥,网站会告诉您未经授权。按照说明获取密钥,然后将其添加到查询参数中。
http://api.openweathermap.org/data/2.5/forecast/daily?APPID=12345&q=...
Error:
Invalid API key. Please see http://openweathermap.org/faq#error401 for more info
错误:无效的API密钥。有关更多信息,请访问http://openweathermap.org/faq#error401
API calls responds with 401 error:
You can get the error 401 in the following cases:
API调用返回401错误:在以下情况下会得到401错误:
here are some steps to find problem.
以下是查找问题的一些步骤。
1) Check if API key is activated
some API services provide key information in dashboard whether its activated, expired etc. openWeatherMap don't.
to verify whether your key is working 'MAKE API CALL FROM BROWSER'
api.openweathermap.org/data/2.5/weather?q=peshawar&appid=API_key
一些应用编程接口服务在仪表板中提供关键信息,无论其是否激活、过期等。OpenWeatherMap不提供。要验证您的密钥是否有效,请从浏览器进行应用编程接口调用‘api.openweathermap.org/data/2.5/weather?q=peshawar&appid=API_key
replace API_key with your own key, if you get data successfully then your key is activated otherwise wait for few hours to get key activated.
将API_KEY替换为您自己的密钥,如果您成功获取数据,则您的密钥被激活,否则等待几个小时才能激活密钥。
2) Check .env for typos & syntax
.env is file which is used to hide credentials such as API_KEY in server side code.
make sure your .env file variables are using correct syntax which is
NAME=VALUE
.env是用于在服务器端代码中隐藏API_KEY等凭据的文件。确保您的.env文件变量使用正确的语法,即name=Value
API_KEY=djgkv43439d90bkckcs
no semicolon, quotes etc
没有分号、引号等
3) Check request URL
check request url where API call will be made , make sure
检查将进行API调用的请求URL,确保
- It doesn't have spaces, braces etc
- correct according to URL encoding
- correct according to API documentation
4) Debug using dotenv:
to know if you dotenv package is parsing API key correctly use the following code
要知道dotenv包是否正确解析了API密钥,请使用以下代码
const result = dotenv.config()
if (result.error) {
throw result.error
}
console.log(result.parsed)
this code checks if .env file variables are being parsed, it will print API_KEY value if its been parsed otherwise will print error which occur while parsing.
这段代码检查.env文件变量是否正在被解析,如果它被解析,它将打印API_KEY值,否则它将打印在解析时发生的错误。
Hopefully it helps :)
希望有帮助:)
I also faced the same issue, I have just created an account on open weather map and also verified the email, tried to load the api using several different url , but they replied with 401 , api key not found.
我也面临着同样的问题,我刚刚在开放天气地图上创建了一个账号,也验证了电子邮件,试图使用几个不同的URL加载API,但他们回复了401,找不到API密钥。
Solution: after 1 hour they all started working, so the reason was for activation it took 1 or some more hours.
解决方案:1小时后,它们都开始工作,所以激活的原因是需要1个小时或更多的时间。
For a graduate i was helping, he had a correct api key and it was active, but the api was incorrectly 401 when no content type was given
对于我正在帮助的一个毕业生,他有一个正确的api键,并且它是活动的,但是当没有给出内容类型时,api错误地为401
it was a simple as adding a Content-Type: application/json, and hey presto the api started working
这只是一个简单的添加Content-Type:Application/json,然后API就开始工作了
curl command
CURL命令
curl --location \
--request GET \
'https://api.openweathermap.org/data/2.5/forecast?lat=55&lon=-3&appid=xxx' \
--header 'Content-Type: application/json'
The api key not set in your url ! before all you must register in https://openweathermap.org/ then get api key in your pesrsonal account after that do it like this:
http://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY_HERE&units=metric
您的url中未设置API密钥!首先,你必须在https://openweathermap.org/中注册,然后在你的悲观账户中获得api密钥,然后像这样做:http://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY_HERE&units=metric
replace you apikey code with {YOUR_API_KEY_HERE}
then run your app.
用{Your_API_Key_Here}替换你的apiKey代码,然后运行你的应用程序。
After registering, you need to verify email.
注册后,您需要验证电子邮件。
On the website API docs, it tells you to connect to the API using this URL:
"https://api.openweathermap.org/data/3.0/onecall?lat=$latitude&lon=$longitude&appid=$apiKey"
在网站API文档上,它会告诉您使用以下URL连接到API:“https://api.openweathermap.org/data/3.0/onecall?lat=$latitude&lon=$longitude&appid=$apiKey”
However, after you've verified your email, you will receive another email informing you of your API key as well as an example of the API call URL:
"https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longtitude&APPID=$apiKey"
但是,在您验证您的电子邮件后,您将收到另一封电子邮件,通知您您的API密钥以及API调用URL的示例:“https://api.openweathermap.org/data/2.5/weather?lat=$latitude&lon=$longtitude&APPID=$apiKey”
I am on the free subscription and have tried the 1st URL multiple times but access was always denied. It only started working after I used the 2nd URL (Provided to me in the 2nd email).
我是免费订阅的,已经尝试了第一个URL多次,但访问总是被拒绝。它只是在我使用第二个URL(在第二封电子邮件中提供给我)后才开始工作。
Sometimes it also happens because of wrong naming of parameter in url.
有时也会因为URL中的参数命名错误而发生这种情况。
url = f"https://api.openweathermap.org/data/2.5/forecast?lat={latitude}&lon={longitude}&appid={api_key}"
In my case I was giving onecall in place "forecast". This typeof silly mistakes also happened sometime. Thanks.
在我的案例中,我给OneCall做的是“预测”。这种愚蠢的错误有时也会发生。谢谢。
You must have a valid API key by registering with openweathermap and confirming your email AND make sure you are compliant with their latest API.
您必须拥有有效的API密钥,方法是注册OpenWeatherMap并确认您的电子邮件,并确保您符合他们的最新API。
A valid API key via registering is applied by appending the key to your URL with &appid=<yourkey>
通过注册来应用有效的API密钥,方法是使用&appid=
将密钥附加到您的URL
Make sure the REST API URL is compliant with their current API specification found at https://openweathermap.org/current#geo.
确保REST API URL符合其在https://openweathermap.org/current#geo.上找到的当前API规范
They have made refactoring recently and removed some parts such as "daily" from the API so also results in an error 401 if added so remove it.
他们最近进行了重构,并从API中删除了一些部分,如“Daily”,因此如果添加了错误401,也会导致错误401,因此删除它。
So change your url to :
url = "http://api.openweathermap.org/data/2.5/forecast?q=London&cnt=10&mode=json&units=metric&appid=<yourkey>
因此将您的URL更改为:url=“http://api.openweathermap.org/data/2.5/forecast?q=London&cnt=10&mode=json&units=metric&appid=
now you have to get paid plan to acess daily forecast:
现在你必须获得付费计划才能访问每日预报:
更多回答
you can skip .env steps if you aren't using dotenv package to hide API keys
如果您没有使用dotenv包隐藏API密钥,则可以跳过.env步骤
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
这并没有真正回答这个问题。如果您有不同的问题,可以通过单击提问来提问。要在此问题获得新答案时收到通知,您可以按照此问题进行操作。一旦你有了足够的声誉,你也可以添加赏金来吸引更多的人关注这个问题。-摘自评论
@isaactfa Actually, it may be a valid answer to the question. The OpenWeatherMap FAQ even lists it as one of the possible solutions to a 401 error: "Your API key is not activated yet. Within the next couple of hours, it will be activated and ready to use."
@isaactfa实际上,这可能是这个问题的有效答案。OpenWeatherMap FAQ甚至将其列为401错误的可能解决方案之一:“您的API密钥尚未激活。在接下来的几个小时内,它将被激活并准备好使用。”
This is the correct answer for me
这就是我的正确答案
This worked for me, but the key was to wrap the URL in quotes. The --request
and --header
flags were not necessary to get things working for me.
这对我很有效,但关键是用引号将URL括起来。--请求和--标头标志对于我来说并不是工作所必需的。
我是一名优秀的程序员,十分优秀!