PayPal - 无效的来电者,需要client_id&秘密

时间:2016-03-25 18:40:24

标签: paypal

我试图向Paypal请求获取新的访问令牌,但在尝试简单的卷曲调用时,它会返回此错误 -

" 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);
                }

1 个答案:

答案 0 :(得分:0)

此解决方案有效 -

    $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 );