Calling Inventory via PHP and these refresh tokens keep expiring?

As a test, I used this and it seemed to work but it seems like these access tokens expire? How do I keep it to refresh and keep the token live? Do I need to even refresh it? I created a token last night and it was expired today. I created another one, pull this call and it returned a response.
<?php
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.lightspeedapp.com/API/Account/{accountID}/Item.json?load_relations=["ItemShops"]&ItemShops.timeStamp=>,2017-11-10T12:00:00Z",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer {Access Token}"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
Super Cat Lightspeed
2 comments
You do need to refresh your access tokens. You can do this by sending a POST request to the access token endpoint. The specific headers that need to be included in the request are detailed here:
https://developers.lightspeedhq.com/retail/authentication/refresh-token/
Since you'll likely want to automate the refreshing of your tokens, you' might want to set a timer that is generated at the same time as you receive your response from to check the expires_in object value in the response body.
Alternatively, you could make a request failure function to send a POST request to the access token endpoint if you receive a response body returning a 401 and access token failure
Adrian Samuel
Software Developer
Lightspeed HQ