PayPal REST API:如何使用axios或curl编码clientId和密码?

问题描述

这是我一直关注的官方的“与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”:“客户端身份验证失败”}

我尝试从前端调用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(未经授权)

解决方法

=SUM(INDEX(SPLIT(TRANSPOSE(SPLIT(B2,CHAR(10))),"- ",0),2) 需要进行base64编码

在JavaScript中,您可以使用${clientId}:${secretKey},toString('base64')或with axios设置btoa()键。

使用curl可以使用auth命令行参数。


对于常规rest API的用法,当以这种方式将基本auth传递给oauth2 / token端点时,您的查询字符串应为-u,如documented here。这将返回您可以在所有其他API调用中使用的access_token。

您链接到的“与PayPal连接”文档用于特定的集成,您需要授权码,但没有其他API使用该授权码。