2
我被一段使OnePageCRM API驗證的代碼卡住了。我已經嘗試示例代碼(Code sample)和我自己的代碼,但是,兩者都得到相同的響應(401)以JSON格式如下回應:OnePageCRM與PHP的API集成
{"error_name":"authorization_data_not_found","status":401,"message":"Authorization
data not found","error_message":"Could not find more helpful message,
sorry.","errors":{}}
只是爲了確保我的登錄憑據是正確的,我已經測試了它here。
OnePageCRM文檔只是指出401狀態是一個登錄問題,但我無法通過第一個請求傳遞登錄數據的請求。
手機號碼代碼:
if(empty($this->api_login) || empty($this->api_password)){
LogReport::write('Unable to send request to OpenPageCRM. API login or password data not provided. Error throw at ' . __FILE__ . ':' . __LINE__);
return;
}
// login to API.
$data = $this->make_api_call('auth/login.json', 'POST', array('login' => $this->api_login, 'password' => $this->api_password));
if($data == null || !isset($data->data)){
LogReport::write('Unable to login to OpenPageCRM. API login or password data provided could not be validated. Error throw at ' . __FILE__ . ':' . __LINE__ .
' Data: ' . $data . ' | ' . $this->api_login . ' x ' . $this->api_password);
return;
}
$uid = $data->data->uid;
$key = base64_decode($data->data->key);
而且處理代碼:
private function make_api_call($url, $http_method, $post_data = array(), $uid = null, $key = null) {
require_once BASE_CLASS . 'class-log.php';
$full_url = 'https://app.onepagecrm.com/api/v3/'.$url;
if(!$ch = curl_init($full_url)){
LogReport::write('Unable to initialize curl at ' . __FILE__ . ':' . __LINE__);
return;
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http_method);
$timestamp = time();
$auth_data = array($uid, $timestamp, $http_method, sha1($full_url));
$request_headers = array();
// For POST and PUT requests we will send data as JSON
// as with regular "form data" request we won't be able
// to send more complex structures
if($http_method == 'POST' || $http_method == 'PUT'){
$request_headers[] = 'Content-Type: application/json';
$json_data = json_encode($post_data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$auth_data[] = sha1($json_data);
}
// Set auth headers if we are logged in
if($key != null){
$hash = hash_hmac('sha256', implode('.', $auth_data), $key);
$request_headers[] = "X-OnePageCRM-UID: $uid";
$request_headers[] = "X-OnePageCRM-TS: $timestamp";
$request_headers[] = "X-OnePageCRM-Auth: $hash";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
if(!$res = curl_exec($ch)){
LogReport::write('Unable to execute CUrl at ' . __FILE__ . ':' . __LINE__);
return;
}
$result = json_decode($res);
curl_close($ch);
if($result->status > 99){
echo "API call error: {$result->message}\n";
LogReport::write('OnePageCRM API call error: ' . $result->message . ' for result ' . $res . ' at ' . __FILE__ . ':' . __LINE__);
return null;
}
return $result;
}
任何意見讚賞。