[英]OnePageCRM API integration with PHP
I'm stuck with a piece of code which makes authentication to OnePageCRM API. 我陷入了一段代码,无法对OnePageCRM API进行身份验证。 I've try the sample code ( Code sample ) and, my own code but, both are getting the same response ( 401 ) with the following response in JSON format: 我已经尝试了示例代码( Code sample ),以及我自己的代码,但是两者都得到了相同的响应(401),并且具有以下JSON格式的响应:
{"error_name":"authorization_data_not_found","status":401,"message":"Authorization
data not found","error_message":"Could not find more helpful message,
sorry.","errors":{}}
Just to make sure my login credentials are correct, I've test it here . 为了确保我的登录凭据正确无误,我在这里进行了测试。
The OnePageCRM documentation just states that 401 status is a login issue but, I can't get through the first request which requires the login data to be passed. OnePageCRM文档只是指出401状态是一个登录问题,但是,我无法通过第一个要求传递登录数据的请求。
My caller code: 我的呼叫者代码:
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);
And the handler code: 和处理程序代码:
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;
}
Any advice appreciated. 任何建议表示赞赏。
尝试仅对login.json而不是auth / login.json进行cURL调用:
$data = $this->make_api_call( 'login.json', 'POST', array('login' => $this->api_login, 'password' => $this->api_password));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.