如何解决PayPal REST API 在 react-native 中获取 access_token,“AUTHENTICATION_FAILURE”
我正在尝试按照 https://medium.com/zestgeek/paypal-integration-in-react-native-9d447df4fce1 链接将 PayPal 集成到我的 react-native 应用程序中。使用邮递员,我得到了回复,但使用我的代码却出现了以下错误:
{"links": [{"href": "https://developer.paypal.com/docs/api/overview/#error","rel": "information_link"}],"message": "由于身份验证凭据无效或授权标头丢失,身份验证失败。","name": "AUTHENTICATION_FAILURE"}
预期响应(使用 Postman - 成功案例)
{
"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 编码的表单数据的语法错误。
因此,没有为您的调用设置请求标头和表单发布数据(在正文中)——它评估并作为对端点的空白请求运行,因此出现错误。
方法如下
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)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。