使用 REST API 连接到 PayPal 时出错

Error while connecting to PayPal using REST API

在令牌 api 调用期间使用沙盒凭据上的 paypal 连接时,我总是得到错误

Array ( [error] => invalid_client [error_description] => Client Authentication failed ) 

下面显示的是我正在使用的 CURL 调用。请帮帮我

       $code = $_GET['code'];
       $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/oauth2/token');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=".$code."");
        
        $headers = array();
        $headers[] = 'Authorization: Basic client_id:client_secret';
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        
        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close($ch);
        $output = json_decode($result,true);
        print_r($output);

如评论所述,CURLOPT_USERPWD 是最简单的策略,让 curl 为您完成工作

如果您要自己设置授权 header,client_id:client_secret 需要进行 Base64 编码


附带说明一下,在命令行上使用 curl 时,等效于 -u 标志