javascript - 使用开放天气图 api/ 时出现 401 错误
问题描述
我正在尝试制作天气应用程序,但出现此错误。 错误
enter code here
const express =require("express");
const https=require("https");
const bodyParser=require("body-parser");
const app=express();
app.use(bodyParser.urlencoded({extended: true}));
app.get("/",function(req,res){
res.sendFile(__dirname+"/index.html");
});
app.post("/",function(req,res)
{
const query=req.body.CityName;
const api_key="658767b0ae936b022f59a69f44868419"
const unit="metric";
const url="https://api.openweathermap.org/data/2.5/weather?q="+query+"&appid="+"&units="+unit;
https.get(url,function(response){
console.log(response.statusCode);
response.on("data",function(data)
{
const weather_data= JSON.parse(data);
console.log(weather_data);
const temp=weather_data.main.temp
const icon=weather_data.weather[0].icon
const icon_id=" http://openweathermap.org/img/wn/"+icon+"@2x.png"
res.write("Temp="+temp);
res.write("<img src="+icon_id+">");
res.send();
})
});
})
API 密钥有效,因为我尝试使用它(https://api.openweathermap.org/data/2.5/weather?q=Trivandrum&appid=658767b0ae936b022f59a69f44868419&units=metric)
解决方案
欢迎来到stackoverflow。
您已经定义了一个名为 api_key 的常量:
const api_key="658767b0ae936b022f59a69f44868419"
但你从不使用它。
您需要更改您的查询以包含您的 api_key 作为 appid:
const url="https://api.openweathermap.org/data/2.5/weather?q="+query+"&appid="+api_key+"&units="+unit;
推荐阅读
- c++ - 创建最多 10^12 的数组
- javascript - 在 JS 数组中创建索引
- node.js - Node.js Mongodb GraphQL - 突变和查询
- arrays - 使用 PowerShell 将 CSV 导入可变大小的二维数组
- node.js - npm-debug 模块:如何以编程方式启用和禁用调试目标?
- discord.js - console.log 消息所有者的显示名称时出错
- r - setDefaultClusterOptions 中的错误(type = .sfOption$type):找不到函数“setDefaultClusterOptions”
- python - python中带有变量的多项式的双积分
- swift - 我收到错误消息 @escaping 属性仅适用于函数类型,即使它以前有效
- typescript - 如何在忽略所有错误的情况下编译 TypeScript 项目?