我正在尝试使用 pagSeguro 的结账 API。我已经在沙箱上完成了注册并获得了我的令牌,我不知道我是否应该做其他事情,但是在我注册并获得我的令牌之后,我尝试在 API 示例中使用它,它给了我以下错误:
{
error_messages: [
{
error: 'invalid_authorization_header',
description: 'Invalid credential. Review AUTHORIZATION header.'
}
]
}
我的代码:
const options = {
method: 'POST',
headers: {
accept: 'application/json',
Authorization: 'xxxx',
'Content-type': 'application/json'
},
body: JSON.stringify({
reference_id: 'cp-000001',
expiration_date: '2023-08-14T19:09:10-03:00',
customer: {
name: 'João test',
email: '[email protected]',
tax_id: '00000000000',
phone: {country: '+55', area: '27', number: '999999999'}
},
customer_modifiable: true,
items: [
{reference_id: 'ITEM01', name: 'item name', quantity: 1, unit_amount: 500}
],
additional_amount: 0,
discount_amount: 0,
shipping: {
type: 'FREE',
amount: 0,
service_type: 'PAC',
address: {
country: 'BRA',
region_code: 'SP',
city: 'São Paulo',
postal_code: '01452123',
street: 'Faria Lima',
number: '12',
locality: 'Pinheiros',
complement: '4 floor'
},
address_modifiable: true,
box: {dimensions: {length: 15, width: 10, height: 14}, weight: 300}
},
payment_methods: [
{type: 'credit_card', brands: ['mastercard']},
{type: 'credit_card', brands: ['visa']},
{type: 'debit_card', brands: ['visa']},
{type: 'PIX'}
],
payment_methods_configs: [
{
type: 'credit_card',
brands: ['mastercard'],
config_options: [{option: 'installments_limit', value: '10'}]
}
],
soft_descriptor: 'xxxx',
redirect_url: 'https://pagseguro.uol.com.br',
return_url: 'https://pagseguro.uol.com.br',
notification_urls: ['https://pagseguro.uol.com.br']
})
};
fetch('https://sandbox.api.pagseguro.com/checkouts', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
有人知道我要做什么吗?
我尝试使用不同的东西代替令牌,但它们也不起作用。
显然您没有遵循正确的授权标头格式。检查他们的文档以获取更多信息。 https://developers.international.pagseguro.com/docs/ payment-page-api
您将看到 API 期望授权标头采用特定格式: 将您的电子邮件和令牌连接起来,并用冒号 (:) 分隔。 对连接的字符串进行 Base64 编码。 将字符串“Basic”添加到编码字符串前面。
如果您不遵守,您的请求将被拒绝。
您应如何执行请求的示例:
const email = '[email protected]';
const token = 'your_pagseguro_token';
const authString = `${email}:${token}`;
const encodedAuth = btoa(authString);