Nodejs中Axios API Post请求错误



我正在尝试对PaystackAPI进行API调用。我使用Axios包node.js。我似乎不能使它工作。

下面是我的代码:
const response = await axios({
method: 'post',
url:'https://api.paystack.co/transaction/initialize',
data: paymentDetails,
headers : {
Authorization: 'mysecretKey',
'content-type': 'application/json',
'cache-control': 'no-cache'
},
})
return res.json(response)

在邮差上,我只是按Paystack的要求提供payment details via the body:

"id": 1,
"username": "kings",
"email": "emailaddress",
"amount": 100

我得到这个错误:AxiosError: Request failed with status code 401

你可能把Authorization弄错了。

试试这个:

const response = await axios({
method: 'post',
url:'https://api.paystack.co/transaction/initialize',
data: paymentDetails,
headers : {
Authorization: `Bearer ${mysecretKey}`,
'content-type': 'application/json',
'cache-control': 'no-cache'
},
})
return res.json(response)

注意Authorization: Bearer {secret}

https://paystack.com/docs/api/验证

相关内容

最新更新