簡體   English   中英

PHP cURL OAuth v1.0

[英]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"}

非常感謝任何幫助/建議。

謝謝你。

在遵循此處的 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM