infusionsoft / infusionsoft-php

PHP client library for the Infusionsoft API.
https://developer.infusionsoft.com/
Other
129 stars 126 forks source link

Error trying to refresh token when token is expired #288

Open marcemarin opened 3 years ago

marcemarin commented 3 years ago

Call to a member function getRefreshToken() on null in /home/forge/default/vendor/infusionsoft/php-sdk/src/Infusionsoft/Infusionsoft.php on line 312

Ultimater commented 2 years ago

The $infusionsoft->refreshAccessToken() method calls: $this->getToken()->getRefreshToken() on line 312. Based on the error you're seeing, it looks like $infusionsoft->getToken() is returning NULL.

Make sure you're setting $infusionsoft->setToken($token) first and passing in an instance of the Infusionsoft/Token class for the $token parameter.

On the README, is shown you can use the $_SESSION following authenticating, to store the token information, and serialize the $infusionsoft->getToken() instance of the Infusionsoft/Token class which is a wrapper to hold your accessToken, refreshToken, and expiration information.

While you wouldn't actually store those in the $_SESSION for a production, you'd probably use a database, the file system, redis, or some other means of storage. But once you store there following authenticating, you should be able to later pull up all the token information (accessToken, refreshToken, expiration, and optionally extra information), and create an instance of the Infusionsoft/Token class, where it would act as a wrapper and hold all that information for you. Then provided you're passing this from storage to $infusionsoft->setToken($token) then only will the $infusionsoft->refreshAccessToken() work. This will use the refresh token, refresh the access token and refresh token along with the expiration information. Then just make sure to save the new information into your storage. An easy approach is $infusionsoft->getToken() then just serialize it so you don't have to deal with the Infusionsoft/Token class. Then when you want to wake up in a later request, you'd pull from your storage and just unserialize and your Infusionsoft/Token class instance would be up and running without having to manually set the accessToken, refreshToken, expiration, and optionally extra information.

But yeah, make sure you're calling $infusionsoft->setToken($token) and passing in an instance of the Infusionsoft/Token class containing the token information from your storage, before calling $infusionsoft->refreshAccessToken() otherwise it will be NULL and cause the error you're seeing.