[英]PayPal REST API: how to encode clientId and secret with axios or curl?
Here is the official Connect With PayPal docs that I have been following: https://developer.paypal.com/docs/connect-with-paypal/integrate/#6-get-access-token这是我一直关注的官方 Connect With PayPal 文档: https : //developer.paypal.com/docs/connect-with-paypal/integrate/#6-get-access-token
I have all of the things to succeed: clientId, secretKey and authorizationCode我拥有所有成功的条件:clientId、secretKey 和 authorizationCode
I have called the API via curl the following way:我通过 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}'
But I get {"error":"invalid_client","error_description":"Client Authentication failed"} all the time但我总是收到 {"error":"invalid_client","error_description":"Client Authentication failed"}
And I have tried calling the API from my front end:我尝试从我的前端调用 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);
}
But, it leads to this error: POST https://api.sandbox.paypal.com/v1/oauth2/token 401 (Unauthorized)但是,它会导致此错误:POST https://api.sandbox.paypal.com/v1/oauth2/token 401 (Unauthorized)
${clientId}:${secretKey}
needs to be base64 encoded ${clientId}:${secretKey}
需要 base64 编码
In JavaScript you can use btoa()
, toString('base64'), or with axios set the auth
key.在 JavaScript 中,您可以使用
btoa()
、 toString('base64') 或使用 axios设置auth
密钥。
With curl you can use the -u
command line parameter.使用 curl,您可以使用
-u
命令行参数。
For general rest API usage, when passing a basic auth that way to the oauth2/token endpoint, your query string should be grant_type=client_credentials
, asdocumented here .对于一般的其余 API 使用,当以这种方式将基本身份验证传递给 oauth2/token 端点时,您的查询字符串应为
grant_type=client_credentials
,如此处所述。 This will return the access_token that you can use in all other API calls.这将返回您可以在所有其他 API 调用中使用的 access_token。
The 'Connect with PayPal' documentation you linked to is for that specific integration, which you need an authorization code, but no other API uses that.您链接到的“与 PayPal 连接”文档适用于该特定集成,您需要授权代码,但没有其他 API 使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.