Discussions
Issue with access_token and refresh_token inside servicem8
This is my code to get the access token and refresh token. The webhook I created was working fine around 3 days ago but suddenly stopped when the access token expired. It wasn't able to fetch the refresh token. Can some help me figure out what I am doing wrong. Has somebody faced similar issue when doing an integration. The token.json file holds the information about the access token, refresh_token. Is it a code issue or is it an issue with the Servicem8. Note: The token expires after 3600second. The webhook seems fine I believe.
if(!empty($_REQUEST['code'])){
$token = get_token($_REQUEST['code']);
}else{
$token = get_access_token();
}
define("TOKEN",$token);
function getaccess_token(){
if(file_exists(DIR."/token.json")){
$fp = fopen(DIR."/token.json", "r");
$dt = fread($fp,filesize(DIR."/token.json"));
$token_data = json_decode($dt);
$expires_in = $token_data->expires_in;
$datetime1 = new DateTime($token_data->current_date);
$datetime2 = new DateTime(date("Y-m-d H:i:s"));
$diff = $datetime1->diff($datetime2);
$secs = ((($diff->format("%a") 24) + $diff->format("%H")) _ 60 +
$diff->format("%i")) * 60 + $diff->format("%s");
if($secs > $expires_in){
return refresh_token($token_data->refresh_token);
}else{
return $token_data->access_token;
}
}else{
return authenticate();
}
}
function refresh_token($refresh_token){
$vars = array(
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'redirect_uri' => REDIRECT_URL,
'refresh_token' => $refresh_token,
'grant_type' => 'refresh_token'
);
$url = "https://go.servicem8.com/oauth/access_token";
$c = curl_init($url);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query($vars));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($c));
curl_close($c);
$response->current_date = date("Y-m-d H:i:s");
$fp = fopen(DIR."/token.json", "w");
fwrite($fp, json_encode($response));
fclose($fp);
return $access_token = $response->access_token;
}
function authenticate(){
$url_code = "https://go.servicem8.com/oauth/authorize?";
$vars = array(
'response_type' => 'code',
'scope' => 'read_jobs read_job_contacts read_job_materials read_job_notes read_customers read_customer_contacts read_job_categories read_staff manage_badges',
'client_id' => CLIENT_ID,
'redirect_uri' => REDIRECT_URL
);
$url = $url_code . http_build_query($vars);
header("Location: $url");
}
function get_token($code){
$url_code = "https://go.servicem8.com/oauth/access_token";
$vars = array(
'grant_type' => 'authorization_code',
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'code' => $code,
'redirect_uri' => REDIRECT_URL,
);
$json = json_encode($vars);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_code);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
$response->current_date = date("Y-m-d H:i:s");
$fp = fopen(DIR."/token.json", "w");
fwrite($fp, json_encode($response));
fclose($fp);
return $access_token = $response->access_token;
}