HI,
I’ve been trying to (refresh access token) using newest PHP SDK version existing on github. I couldn’t find any details on the subject in the documentation.
i’m trying to write this code :
<?php
require_once 'vendor/autoload.php';
require_once 'File.php';
require_once 'Config.php';
$infusionsoft = new \Infusionsoft\Infusionsoft(array(
'clientId' => CLIENT_ID,
'clientSecret' => CLIENT_SECRET,
'redirectUri' => URL,
));
// If the serialized token is available in the session storage, we tell the SDK
// to use that token for subsequent requests.
$SavedToken = json_decode(File::GetFileContent('token'));
if (!empty($SavedToken->Value) && (strtotime("now") - strtotime($SavedToken->At)) < 86400 ) {
$infusionsoft->setToken(unserialize($SavedToken->Value));
}
// If we are returning from Infusionsoft we need to exchange the code for an
// access token.
if (isset($_GET['code']) && !$infusionsoft->getToken()) {
File::Truncate('token', json_encode(
array(
"At"=> date('Y-m-d H:i:s'),
"Value" => serialize($infusionsoft->requestAccessToken($_GET['code']))
)
));
$infusionsoft->refreshAccessToken();
echo ('New Access Token Saved');
exit();
}
if ($infusionsoft->getToken()) {
$saved_token = unserialize($SavedToken->Value);
$token = new \Infusionsoft\Token([
'access_token' => $saved_token->accessToken,
'refresh_token' => $saved_token->refreshToken,
'expires_in' => $saved_token->endOfLife,
]);
$infusionsoft->setToken($token);
$infusionsoft->refreshAccessToken();
$token = $infusionsoft->getToken();
echo ('Token Time not finished yet');
exit();
} else {
echo '<a href="' . $infusionsoft->getAuthorizationUrl() . '">Click here to authorize</a>';
exit();
}
but i faced the problem :
PHP Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://api.infusionsoft.com/token` resulted in a `400 Bad Request` response:
{"error":"invalid_grant","error_description":"Invalid refresh token"}
in /home1/autotech/public_html/infusion/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113
Stack trace:
#0 /home1/autotech/public_html/infusion/vendor/guzzlehttp/guzzle/src/Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response))
#1 /home1/autotech/public_html/infusion/vendor/guzzlehttp/promises/src/Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response))
#2 /home1/autotech/public_html/infusion/vendor/guzzlehttp/promises/src/Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\Psr7\Response), Array)
#3 /home1/autotech/public_html/infusion/vendor/guzzlehttp/promises/src/TaskQueue.php(47): GuzzleHttp\Promise\Promise::GuzzleHttp\Promise\{closure}()
in /home1/autotech/public_html/infusion/vendor/infusionsoft/php-sdk/src/Infusionsoft/Http/GuzzleHttpClient.php on line 74
Would you guys guide me in the right direction with a tested code!
Thank you