PayPal REST API:如何使用 axios 或 curl 对 clientId 和 secret 进行编码?
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(未授权)
${clientId}:${secretKey}
需要base64编码
在 JavaScript 中,您可以使用 btoa()
、toString('base64') 或 with axios 设置 auth
键。
通过 curl,您可以使用 -u
命令行参数。
对于一般休息 API 用法,当以这种方式将基本身份验证传递到 oauth2/token 端点时,您的查询字符串应为 grant_type=client_credentials
,如 documented here。这将 return 您可以在所有其他 API 调用中使用的 access_token。
您链接到的 'Connect with PayPal' 文档是针对该特定集成的,您需要授权码,但没有其他 API 使用它。
这是我一直关注的官方 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(未授权)
${clientId}:${secretKey}
需要base64编码
在 JavaScript 中,您可以使用 btoa()
、toString('base64') 或 with axios 设置 auth
键。
通过 curl,您可以使用 -u
命令行参数。
对于一般休息 API 用法,当以这种方式将基本身份验证传递到 oauth2/token 端点时,您的查询字符串应为 grant_type=client_credentials
,如 documented here。这将 return 您可以在所有其他 API 调用中使用的 access_token。
您链接到的 'Connect with PayPal' 文档是针对该特定集成的,您需要授权码,但没有其他 API 使用它。