node.js - 得到 - HTTPS 请求 - HTTPError
问题描述
我正在尝试使用下面的代码段访问受基本身份验证保护的 URL。
const got = require('got');
export const getToken = async () => {
const payload = new URLSearchParams({grant_type: 'client_credentials'}).toString();
const auth = "Bearer " + Buffer.from(process.env.username + ":" + process.env.password).toString('base64');
try{
const response = await got(process.env.url, {
body: payload,
method: "POST",
headers: {
"Authorization" : auth,
"Content-Type": "application/x-www-form-urlencoded",
"Content-Length": payload.length.toString()
}
});
}catch(error){
console.log(`ERROR: ${JSON.stringify(error)}`);
}
};
但这会导致错误,也没有提供任何细节。
{"name":"HTTPError","timings":{"start":1634484011218,"socket":1634484011219,"lookup":1634484011230,"connect":1634484011248,"secureConnect":1634484011288,"upload":1634484011288,"response":1634484011307,"end":1634484011310,"abort":1634484011314,"phases":{"wait":1,"dns":11,"tcp":18,"tls":40,"request":0,"firstByte":19,"download":3,"total":96}}}
任何有关解决问题的建议将不胜感激。谢谢。
解决方案
推荐阅读
- html - Apex - 通过字符串生成 HTML 表单
- javascript - 自定义 NPM UI 库,未找到模块
- php - 试图让 PDO 加载
- swift - 从 Swift 中的 href 中提取链接
- html - 如何使 rmarkdown ioslides 宽屏
- javascript - 试图在反应中为门户呈现一个按钮并得到这个错误:× TypeError: Object(...) is not a function on the modalwrapper style
- c# - 在 Chrome 版本 74 中使用 Selenium:发生意外的 NoSuchElementException
- javascript - Gatsby:单击路线内锚元素会导致重新渲染
- javascript - 尝试使用圆形工具提示创建折线图
- python - 如何使用 squish 自动化工具从 Windows 资源管理器弹出窗口上传文件