OnePageCRM API与PHP的集成
我被一段代码卡住了,这段代码对OnePageCRM API进行了身份验证。我已经尝试了示例代码()和我自己的代码,但是,它们都得到了相同的响应(401),以下是JSON格式的响应:OnePageCRM API与PHP的集成,php,Php,我被一段代码卡住了,这段代码对OnePageCRM API进行了身份验证。我已经尝试了示例代码()和我自己的代码,但是,它们都得到了相同的响应(401),以下是JSON格式的响应: {"error_name":"authorization_data_not_found","status":401,"message":"Authorization data not found","error_message":"Could not find more helpful message, sorry.
{"error_name":"authorization_data_not_found","status":401,"message":"Authorization
data not found","error_message":"Could not find more helpful message,
sorry.","errors":{}}
为了确保我的登录凭据是正确的,我已经对它进行了测试
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;
}
非常感谢您的建议。尝试对login.json而不是auth/login.json进行cURL调用:
$data = $this->make_api_call( 'login.json', 'POST', array('login' => $this->api_login, 'password' => $this->api_password));