【发布时间】:2021-11-08 15:44:58
【问题描述】:
我正在尝试使用basic authentication 流从邮递员的 api 端点获取访问令牌。
app.post('/epic', async (req:Request, res) => {
const code = req.query.code as string
const url = "https://api.epicgames.dev/epic/oauth/v1/token"
const values = new URLSearchParams({
code,
client_id,
client_secret,
scope: "basic_profile",
grant_type: "authorization_code",
})
console.log(code, values)
try {
const res = await axios.post(url, values, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
})
console.log(res.data)
return res.data
} catch (error: any) {
console.error(error.message)
throw new Error(error.message)
}
})
它不断返回 400 错误请求。我是不是做错了什么?
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()
【问题讨论】:
-
分享更多错误信息
-
UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺
-
查看控制台,axios.post 调用抛出了什么错误。所以,分享这个的输出:
console.error(error.message)来自你的 catch 块 -
它说“请求失败,状态码为 400”并且代码参数未定义。但我是从邮递员那里传过来的。谢谢
-
它是否在您的控制台上正确打印代码?我看到
console.log(code, values),这是登录控制台吗?你怎么从邮递员那里调用API,你能分享一下
标签: javascript node.js api axios http-status-code-400