PayPal REST API 获取 react-native 中的 access_token,“AUTHENTICATION_FAILURE”
Posted
技术标签:
【中文标题】PayPal REST API 获取 react-native 中的 access_token,“AUTHENTICATION_FAILURE”【英文标题】:PayPal REST API fetch for access_token in react-native, "AUTHENTICATION_FAILURE" 【发布时间】:2021-05-24 16:29:29 【问题描述】:我正在尝试通过https://medium.com/zestgeek/paypal-integration-in-react-native-9d447df4fce1 链接将 PayPal 集成到我的 react-native 应用程序中。使用邮递员我得到了响应,但是使用我的代码却给出了这个错误:
“链接”:[“href”:“https://developer.paypal.com/docs/api/overview/#error”,“rel”:“information_link”],“消息”:“由于身份验证凭据无效或缺少授权标头,身份验证失败。", "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)
)
【问题讨论】:
【参考方案1】:错误的获取语法(为什么要单独的对象参数?)以及在作为主体的对象部分中的 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)
【讨论】:
以上是关于PayPal REST API 获取 react-native 中的 access_token,“AUTHENTICATION_FAILURE”的主要内容,如果未能解决你的问题,请参考以下文章
Paypal Rest API - 如何获取客户端 ID 和密码?
是否有一个等效的 REST API 函数来获取 Paypal 帐户的余额,类似于 NVP 获取余额 API? [关闭]
需要帮助使用 PayPal REST API 或 C# 中的任何其他方法获取 PayPal 交易