【问题标题】:Error while connecting to PayPal using REST API使用 REST API 连接到 PayPal 时出错
【发布时间】:2020-11-14 02:43:56
【问题描述】:

在令牌 api 调用期间在沙盒凭据上使用 connect with 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(如client_id:client_secret。)

标签: php curl paypal-sandbox


【解决方案1】:

正如评论中提到的,CURLOPT_USERPWD 是最简单的策略,让 curl 为您完成工作

如果您要自己设置 Authorization 标头,client_id:client_secret 需要进行 Base64 编码


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

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 2017-09-10
    • 2016-02-09
    • 2014-06-19
    • 2021-12-14
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多