XeroAPI / xero-php-oauth2

Xero PHP SDK for oAuth 2 generated from Xero API OpenAPI Spec 3.0
MIT License
91 stars 65 forks source link

Unable to generate refresh token #232

Closed vijayadhithyanmohan1701 closed 3 years ago

vijayadhithyanmohan1701 commented 3 years ago

Unable to generate refresh token A clear and concise description of what the bug is.

Steps followed Hi all, I am unable to create the refresh token for some reasons. I am using the Storage class to retrieve tokens and connect with API . I followed the same steps as instructed. The refresh token that should be generated every 30 minutes is not happening at all.

Code Excerpt ini_set('display_errors', 'On'); session_start();

include DIR . '/vendor/autoload.php'; require_once('storage.php');

use XeroPHP\Application; use XeroPHP\Models\Accounting\Account; use XeroPHP\Models\Accounting\Address; use XeroPHP\Models\Accounting\BrandingTheme; use XeroPHP\Models\Accounting\Contact; use XeroPHP\Models\Accounting\Invoice; use XeroPHP\Models\Accounting\Invoice\LineItem; use XeroPHP\Models\Accounting\Payment; use XeroPHP\Webhook;

$storage = new StorageClass(); $xeroTenantId = (string)$storage->getSession()['tenant_id']; echo "Acc Token: ".$storage->getSession()['token']."

"; echo "Refresh Token: ".$storage->getRefreshToken()."

"; echo "Expiry time: ". date('Y-m-d H:i:s', $storage->getExpires());

if ($storage->getHasExpired()) { echo "
Expired
";

$provider = new \League\OAuth2\Client\Provider\GenericProvider([ 'clientId' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'clientSecret' => 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy', 'redirectUri' => 'http://localhost:8888/xero-int/callback.php', 'urlAuthorize' => 'https://login.xero.com/identity/connect/authorize', 'urlAccessToken' => 'https://identity.xero.com/connect/token', 'urlResourceOwnerDetails' => 'https://api.xero.com/api.xro/2.0/Organisation' ]); echo '

';
var_dump($storage->getSession());
echo '
';

echo $storage->getRefreshToken(); $newAccessToken = $provider->getAccessToken('refresh_token', [ 'grant_type' => 'refresh_token', 'refresh_token' => $storage->getRefreshToken() ]);

// Save my token, expiration and refresh token $storage->setToken( $newAccessToken->getToken(), $newAccessToken->getExpires(), $xeroTenantId, $newAccessToken->getRefreshToken(), $newAccessToken->getValues()["id_token"] );

  echo "New Acc Token: ".$storage->getToken()['token']."<br><br>";
  echo "New Expiry time: ". date('Y-m-d H:i:s', $newAccessToken->getExpires());

}

Additional context I am using echo and var_dump to check if I am able to get the previously stored token, refresh_token etc. which I am. But I am unable to get the refresh token. Kindly let me know if i should be doing something else.

Error Code Fatal error: Uncaught League\OAuth2\Client\Provider\Exception\IdentityProviderException: invalid_grant in /Users/macuser/Dropbox/My Mac (Apple-Mac-121.local)/Desktop/Sites/xero-int/vendor/league/oauth2-client/src/Provider/GenericProvider.php:222 Stack trace: #0 /Users/macuser/Dropbox/My Mac (Apple-Mac-121.local)/Desktop/Sites/xero-int/vendor/league/oauth2-client/src/Provider/AbstractProvider.php(628): League\OAuth2\Client\Provider\GenericProvider->checkResponse(Object(GuzzleHttp\Psr7\Response), Array) #1 /Users/macuser/Dropbox/My Mac (Apple-Mac-121.local)/Desktop/Sites/xero-int/vendor/league/oauth2-client/src/Provider/AbstractProvider.php(537): League\OAuth2\Client\Provider\AbstractProvider->getParsedResponse(Object(GuzzleHttp\Psr7\Request)) #2 /Users/macuser/Dropbox/My Mac (Apple-Mac-121.local)/Desktop/Sites/xero-int/apiops.php(42): League\OAuth2\Client\Provider\AbstractProvider->getAccessToken(Object(League\OAuth2\Client\Grant\RefreshToken), Array) #3 /Users/macuser/Dropbox/My Mac (Apple-Mac-121.local)/Desktop/Sites/xer in /Users/macuser/Dropbox/My Mac (Apple-Mac-121.local)/Desktop/Sites/xero-int/vendor/league/oauth2-client/src/Provider/GenericProvider.php on line 222

It is quite frustrating to generate tokens every 30 minutes.

SidneyAllen commented 3 years ago

Hi @vijayadhithyanmohan1701

I recommend looking at our php starter project - it has all the pieces to authorize, obtain an access and refresh token and refresh it when it expires.

https://github.com/XeroAPI/xero-php-oauth2-starter

There also a video that walks through setting up and running the project https://youtu.be/vc1d7vdWeOE

I hope this helps. You will need to include the offline_access scope when authorizing or a refresh token will not be returned.

vijayadhithyanmohan1701 commented 3 years ago

Hi @SidneyAllen

Thanks for your prompt response.

It was my mistake which I didn't notice yesterday. I did create the authorisation function similar to the starter program. For some reasons I was debugging the refresh token inside the try/catch block which I forgot to comment out before sending the credentials to 'functions' file. So it was sending the access token which is correct but it got refreshed before being sent to the 'functions' file. Hence the token

Thanks again.