PayPal REST API 获取,用于在 react-native 中获取access_token,"AUTHENTICATION_FAILURE"



我试图通过以下https://medium.com/zestgeek/paypal-integration-in-react-native-9d447df4fce1链接整合PayPal在我的反应原生应用程序。对于postman,我得到了一个响应,但是对于我的代码,它给出了这个错误:

{"links": [{"href": "https://developer.paypal.com/docs/api/overview/#error"; "rel"; "information_link"}], "message": "认证失败,由于无效的认证凭证或缺少授权头;"name"; "AUTHENTICATION_FAILURE"}

期望响应(使用邮差-成功案例)

{
"scope": "https://uri.paypal.com/services/invoicing https://uri.paypal.com/services/vault/payment-tokens/read https://uri.paypal.com/services/disputes/read-buyer https://uri.paypal.com/services/payments/realtimepayment https://uri.paypal.com/services/disputes/update-seller https://uri.paypal.com/services/payments/payment/authcapture openid https://uri.paypal.com/services/disputes/read-seller Braintree:Vault https://uri.paypal.com/services/payments/refund https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/payments/.* https://uri.paypal.com/payments/payouts https://uri.paypal.com/services/vault/payment-tokens/readwrite https://api.paypal.com/v1/vault/credit-card/.* https://uri.paypal.com/services/subscriptions https://uri.paypal.com/services/applications/webhooks",
"access_token": "A21AAL0lvt6p5QhxL5p6KusbbwMNu8o_DWDyNHHWAc3KuOBK7MO4YY3s_1CTMiY1BCAZwQ-dX7MzixsxY5StFfEFrSI2pamNg",
"token_type": "Bearer",
"app_id": "APP-80W284485P519543T",
"expires_in": 32400,
"nonce": "2021-02-22T11:42:28ZIt4HFB53zPnqZtMo8CeBA17Aj1MKaRQDVAVRE9VqUJg"
}

我的代码出错

fetch('https://api.sandbox.paypal.com/v1/oauth2/token',{ grant_type: 'client_credentials' },
{ 
method: 'POST',
headers: { 
'Accept': 'application/json', 
'Accept-Language': 'en_US',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic QVVydEp4Y0hmTVVsRGpIZ1YyRkhNT1Vuek1rVWV1ODZfa203aDY3dUVIekg1YjVSTjdWby1xOEFZUHRjZHo3SWFpb2M0NnhXMEg5SlFabVQ6RU1iYkotWXFRTFQ2bGl1UHRKVVJxMnBBZ2g5V3VVVERLbVYzNTVfVkllQURzdDBCTWxuVU5LaUhWTEs3aXRDeVpGWHJFUU9leDlwOTNXTzg='
}

}) .then(response => response.json())
.then(async (data) => {
console.log(data)

})
.catch(function (error) {
let edata = error.message;
console.log('Error:', edata)
}
)

获取的语法错误(为什么要分开对象参数?),并且在作为主体的对象部分中使用url编码的表单数据的语法错误。

因此,没有为您的调用设置请求头,也没有表单post数据(在body中)-它计算并作为一个空白请求运行到端点,因此出现错误。

方法如下

fetch('https://api.sandbox.paypal.com/v1/oauth2/token', { 
method: 'POST',
headers: { 
'Accept': 'application/json', 
'Accept-Language': 'en_US',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + btoa('YOUR_CLIENTID:YOUR_SECRET')
},
body: 'grant_type=client_credentials'
}).then(response => response.json())
.then(async (data) => {
console.log(data)
}).catch(function (error) {
let edata = error.message;
console.log('Error:', edata)
}

相关内容

  • 没有找到相关文章

最新更新