首页 > 解决方案 > PHP cURL OAuth v1.0

问题描述

我正在尝试使用 cURL 在 PHP中实现来自 LivePerson ( https://developers.liveperson.com/engagement-history-api-methods.html ) 的参与历史 API 。

它在 Postman 上运行良好,但我无法让它在 PHP 中运行。

这是我的代码,它是类中函数的一部分:

$nonce = sha1(time());
$timestamp = time();

$oauth = new OAuth($this->consumerKey, $this->consumerSecret, OAUTH_SIG_METHOD_HMACSHA1);
$oauth->setVersion('1.0');
$oauth->setToken($this->accessToken, $this->tokenSecret);
$oauth->setTimestamp($timestamp);
$oauth->setNonce($nonce);

//Sets the HTTP Headers for the curl.
$headers = array(
    'Content-Type: application/json',
    'Connection: keep-alive',
    'Keep-Alive: 800000',
    'Authorization: ' . $oauth->getRequestHeader('POST', $url)
);

$live_person_post_data = array(
    "start" => array(
        "from" => 1604174400000,
        "to" => 1604188740000
    )
);

$live_person_post_data_Encoded = json_encode($live_person_post_data);

// Configure curl options in order to retrieve conversations from Live Person.
$opts = array(
    CURLOPT_URL             => $url,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_CUSTOMREQUEST   => 'POST',
    CURLOPT_POST            => 1,
    CURLOPT_POSTFIELDS      => $live_person_post_data_Encoded,
    CURLOPT_HTTPHEADER      => $headers
);

// Initialize curl
$curl = curl_init();

// Set curl options
curl_setopt_array($curl, $opts);

// Get the results
$result = curl_exec($curl);

// Close resource
curl_close($curl);

我尝试了各种各样的事情,我看到的所有例子都或多或少与我上面写的相同,但我不断得到{"code":"0005"}回应。

非常感谢任何帮助/建议。

谢谢你。

标签: phpcurl

解决方案


在此处遵循 OAuth 身份验证 v1.0 文档后,我将代码更改为以下内容:

            $url = "https://lo.enghist.liveperson.net/interaction_history/api/account/{$this->account_id}/interactions/search";
            $url_query = 'limit=100&offset=0&sort=' . urlencode('start:asc');
            $url_query_encoded = 'limit=100&offset=100&sort=' . rawurlencode('start:asc');
            $full_url = $url . '?' . $url_query;
            $nonce = md5(time());
            $timestamp = time();
            
            $oauth_signature_method = 'HMAC-SHA1';
            $signature_key = rawurlencode($this->consumerSecret) . '&' . rawurlencode($this->tokenSecret);
            
            $signature_base_string =
                "POST&" . rawurlencode($url) . "&" .
                rawurlencode(
                    'limit=100'
                    . "&oauth_consumer_key=". $this->consumerKey
                    . "&oauth_nonce=" . $nonce
                    . "&oauth_signature_method=" . $oauth_signature_method
                    . "&oauth_timestamp=" . $timestamp
                    . "&oauth_token=" . $this->accessToken
                    . "&oauth_version=1.0"
                    . "&offset=100"
                    . "&sort=" . rawurlencode('start:asc')
                );
            
            $oauthSig = base64_encode(hash_hmac("sha1", $signature_base_string, $signature_key, true));

            //Sets the HTTP Headers for the curl.
            $headers = array(
                'Content-Type: application/json',//;charset=UTF-8',
                'Authorization: OAuth ' .
                    'oauth_consumer_key="' . $this->consumerKey . '"' .
                    ',oauth_token="' . $this->accessToken . '"' .
                    ',oauth_signature_method="' . $oauth_signature_method . '"' .
                    ',oauth_timestamp="' . $timestamp . '"' .
                    ',oauth_nonce="' . $nonce . '"' .
                    ',oauth_version="1.0"' .
                    ',oauth_signature="' . rawurlencode($oauthSig) . '"'
            );

            $live_person_post_data = array(
                "start" => array(
                    "from" => 1604174400000,
                    "to" => 1604188740000
                )
            );
            
            $live_person_post_data_Encoded = json_encode($live_person_post_data);
            
            // Configure curl options in order to retrieve conversations from Live Person.
            $opts = array(
                CURLOPT_URL             => $full_url,
                CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_1_1,
                CURLOPT_FOLLOWLOCATION  => true,
                CURLOPT_RETURNTRANSFER  => true,
                CURLOPT_CUSTOMREQUEST   => 'POST',
                CURLOPT_POST            => 1,
                CURLOPT_POSTFIELDS      => $live_person_post_data_Encoded,
                CURLOPT_HTTPHEADER      => $headers
            );
            
            // Initialize curl
            $curl = curl_init();
            
            // Set curl options
            curl_setopt_array($curl, $opts);
            
            // Get the results
            $result = curl_exec($curl);

            curl_close($curl);

推荐阅读