【问题标题】:PayPal - Invalid caller, requires client_id & secretPayPal - 调用者无效,需要 client_id 和 secret
【发布时间】:2016-07-13 13:30:10
【问题描述】:

我正在尝试向 Paypal 发出请求以获取新的访问令牌,但在尝试简单的 curl 调用时,它返回此错误 -

“Invalid_request - 调用者无效,需要 client_id 和秘密”

不太清楚这有什么问题。客户端 ID 和密码正确并包含在内。

这是我尝试过的 -

                $clientId = "XXX";
                $secret = "XXX";
                $url = "https://api.sandbox.paypal.com/v1/oauth2/token";
                $method = 'POST';
                $postvals = sprintf("client_id=%s&client_secret=%s&grant_type=%s&refresh_token=%s",
                            $clientId,
                            $secret,
                            'refresh_token',
                            $refreshToken);


                $ch = curl_init($url);

                 if ($method == 'POST'){
                     $options = array(
                        CURLOPT_POST => 1,
                        CURLOPT_POSTFIELDS => $postvals,
                    );
                    curl_setopt_array($ch, $options);
                 } 

                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_HEADER, true); 
                curl_setopt($ch, CURLINFO_HEADER_OUT, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
                curl_setopt($ch, CURLOPT_SSLVERSION, 1); 
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret); 
                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
                curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
                curl_setopt($ch, CURLOPT_VERBOSE, true); 
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                  "Accept: application/json",
                  "Accept-Language: en_US",
                  "Content-Type: application/x-www-form-urlencoded",
                  'Authorization: Bearer '.$old_accessToken,
                  "Host: www.paypal.com",
                  "Connection: close"
                 )
                );

                $result = curl_exec($ch);

                // Get Request and Response Headers
                $requestHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
                $responseHeaderSize = strlen($result) - curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
                $responseHeaders = substr($result, 0, $responseHeaderSize);

                //only use body and not header, otherwise return is NULL
                $result = substr($result, $responseHeaderSize);

                if(!$result || empty($result)){
                    echo 'Curl error or response is empty: ' . curl_error($ch);
                    curl_close($ch);
                } else {
                    curl_close($ch);
                    $response =json_decode($result);
                    var_dump($response);
                }

【问题讨论】:

    标签: paypal


    【解决方案1】:

    这个解决方案反而奏效了 -

        $clientId = "XXXX";
        $clientSecret = "XXXX";
        $url = "https://api.sandbox.paypal.com/v1/oauth2/token";
        $method = 'POST';
        $postdata = sprintf("client_id=%s&client_secret=%s&grant_type=%s&refresh_token=%s",
                                    $clientId,
                                    $secret,
                                    'refresh_token',
                                    $refreshToken);
    
       $curl = curl_init($url); 
       curl_setopt($curl, CURLOPT_POST, true); 
       curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($curl, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
       curl_setopt($curl, CURLOPT_HEADER, false); 
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
       curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); 
       #    curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
       $response = curl_exec( $curl );
                if (empty($response)) {
                    // some kind of an error happened
                    die(curl_error($curl));
                    curl_close($curl); // close cURL handler
                } else {
                    $info = curl_getinfo($curl);
                    echo "Time took: " . $info['total_time']*1000 . "ms\n";
                    curl_close($curl); // close cURL handler
                    if($info['http_code'] != 200 && $info['http_code'] != 201    ) {
                        echo "Received error: " . $info['http_code']. "\n";
                        echo "Raw response:".$response."\n";
                        die();
                    }
                }
       // Convert the result from JSON format to a PHP array 
       $jsonResponse = json_decode( $response );
    

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 2021-05-28
      • 2022-09-17
      • 2016-08-30
      • 2018-05-06
      • 2021-03-02
      • 1970-01-01
      • 2017-04-27
      • 2018-11-23
      相关资源
      最近更新 更多