php - 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);
推荐阅读
- c# - 作为 Windows 服务部署的 ASP .Net Core RESTful 应用程序不工作/启动
- reactjs - 如何使用 React DatePicker 选择时间和日期范围
- mongodb - 如何为 mongodb 动态添加的集合创建索引?
- javascript - 你好!如何从 Vue.js 中的 localStorage 中提取数据并呈现在页面上?
- c++ - WaitNamedPipe() 在 Windows XP 中返回 0 与 2 个以上的客户端
- python - 无法在我的 Windows 10 机器上使用 cx_Freeze
- sql - 我可以将 table1 的每一行加入到 table 2 的唯一行吗?
- c - 如何解决遍历森林中抛出的异常
- android - 如何在flutter中使用Dio将flutter_sound包录制的音频上传到服务器?
- constraints - 如何限制 GAMS 混合整数非线性规划中非零变量的数量?