[英]PayPal REST API: how to encode clientId and secret with axios or curl?
這是我一直關注的官方 Connect With PayPal 文檔: https : //developer.paypal.com/docs/connect-with-paypal/integrate/#6-get-access-token
我擁有所有成功的條件:clientId、secretKey 和 authorizationCode
我通過 curl 調用 API 的方式如下:
curl -X POST https://api.sandbox.paypal.com/v1/oauth2/token \
-H 'Authorization: Basic {clientId:secretKey}' \
-d 'grant_type=authorization_code&code={authorizationCode}'
但我總是收到 {"error":"invalid_client","error_description":"Client Authentication failed"}
我嘗試從我的前端調用 API:
const login = async (code) => {
const config = {
method: 'POST',
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
headers: {
'Authorization': `Basic ${clientId}:${secretKey}`,
'content-type': 'application/x-www-form-urlencoded',
},
data: `grant_type=authorization_code&code=${authorizationCode}`
}
await axios(config);
}
但是,它會導致此錯誤:POST https://api.sandbox.paypal.com/v1/oauth2/token 401 (Unauthorized)
${clientId}:${secretKey}
需要 base64 編碼
在 JavaScript 中,您可以使用btoa()
、 toString('base64') 或使用 axios設置auth
密鑰。
使用 curl,您可以使用-u
命令行參數。
對於一般的其余 API 使用,當以這種方式將基本身份驗證傳遞給 oauth2/token 端點時,您的查詢字符串應為grant_type=client_credentials
,如此處所述。 這將返回您可以在所有其他 API 調用中使用的 access_token。
您鏈接到的“與 PayPal 連接”文檔適用於該特定集成,您需要授權代碼,但沒有其他 API 使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.