Weble / ZohoClient

11 stars 7 forks source link

Refresh Access Token #1

Closed ProgrammerZ closed 3 years ago

ProgrammerZ commented 4 years ago

Could you please help with using offline mode. I successfully get access token with:

$client = new OAuthClient($this->self_client, $this->self_secret);
$client->offlineMode();
$client->useCache($this->cache);
dump($client->getAccessToken());

When the token expires I refresh the token the following way:

$client = new OAuthClient($this->self_client, $this->self_secret);
$client->offlineMode();
$client->useCache($this->cache);    

$client->setRefreshToken($client->getRefreshToken());
$acc_tok = $client->refreshAccessToken();
$client->setGrantCode($acc_tok);

but can't figure out how to check when the access token expires and do it automatically:

$client->accessTokenExpired() always return false, even when token is expired.

I'm still grasping OAuth

bluec commented 4 years ago

Hi @ProgrammerZ did you get anywhere with this? I am also confused by the same issue.

$client->accessTokenExpired() always returns false and $client->accessTokenExpiration seems to always be null.

Did you find a way to reliably determine whether we need to get a new access token?

Skullbock commented 4 years ago

Hey, I’ll get back on this next week if you still have this issue 👌🏻

Il giorno mar 8 set 2020 alle 10:18 Chris N notifications@github.com ha scritto:

Hi @ProgrammerZ https://github.com/ProgrammerZ did you get anywhere with this? I am also confused by the same issue.

$client->accessTokenExpired() always returns false and $client->accessTokenExpiration seems to always be null.

Did you find a way to reliably determine whether we need to get a new access token?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Weble/ZohoClient/issues/1#issuecomment-688703964, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAINRU7IBWAE7VRIEGIKE6DSEXSGFANCNFSM4OCCBPOA .

-- Daniele Rosario CTO

Weble.it http://weble.it/ Via Enrico Fermi 265 36100 Vicenza +39 0444 130094 <+390444130094> +39 328 3017134 <+393283017134>

bluec commented 4 years ago

Thanks @Skullbock that would be useful. I have been reading the code and think I have determined that calls to getAccessToken() will attempt to refresh the access token if it has expired.

So far have come up with this, which seems to work for now at least...

$client->setRefreshToken($client->getRefreshToken());
$client->setGrantCode($client->getAccessToken());

Any further advice on usage would be appreciated.

Skullbock commented 4 years ago

Hey @bluec @ProgrammerZ i checked this.

As you guessed @bluec he code will refresh the code automatically for you when it's expired, so there is no need to do that manually, unless there is a very specific thing that you're trying to to with OAuth.

Even if the token expires "mid call", the client will catch it, and try to refresh the token, rerunning the call to the api again.

This of course only works if you set the refresh token and are working in offline mode. Anyway, i also added a new method to access the token expiration date just for convenience, and added a test to verify that this works.

The code that for me works is:

$client = new OAuthClient($client_id, $client_secret);
$client->setRefreshToken($refresh_token);
$client->setRegion(Region::us());
$client->offlineMode();
$client->useCache($pool); // This is a PSR6 cache pool

Let me know if this helps, so i'll close the issue Cheers!

bluec commented 4 years ago

Thanks @Skullbock I have something very similar, my only question for your example code above is where are you getting $refresh_token from?

I have found I have to assign it from $client->getRefreshToken() which seems a bit counter-intuitive and I think is what was tripping me up. I expected that the client would automatically check the cache for any refresh token when the access token is expired.

Skullbock commented 4 years ago

I that case i'm using the "Self Client" type in the zoho api console, getting the zoho grant code from the ui and, manually, using this library once to get the refresh token. I then store the refresh token on the local configuration, and then set it manually on every api call. Refresh tokens should not be regenerated since they do not expire

bluec commented 4 years ago

Would the refresh token not change when a new access token is generated?

Skullbock commented 4 years ago

Nope, not with zoho. The refresh token is generated, and will be the same. If you generate a new one for the same application, you'll get it, but you can have max 20 of them, and the 21st will delete the 1st

On Wed, Sep 16, 2020 at 10:14 AM Chris N notifications@github.com wrote:

Would the refresh token not change when a new access token is generated?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Weble/ZohoClient/issues/1#issuecomment-693249800, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAINRUZDHCEXTZRIA72ZVA3SGBXXFANCNFSM4OCCBPOA .

-- Daniele Rosario CTO

Weble.it http://weble.it/ Via Enrico Fermi 265 36100 Vicenza +39 0444 130094 <+390444130094> +39 328 3017134 <+393283017134>

bluec commented 4 years ago

Ah ok. So I think my misunderstanding was that I believed the refresh token would be automatically retrieved from the cache if a new access token was required.

However, it does seem ok that I can manually get the refresh token from the cache using $client->getRefreshToken() and set it using $client->setRefreshToken($client->getRefreshToken()) and this will then allow the client to use the refresh token if it is required.

Skullbock commented 4 years ago

Correct

On Wed, Sep 16, 2020 at 10:22 AM Chris N notifications@github.com wrote:

Ah ok. So I think my misunderstanding was that I believed the refresh token would be automatically retrieved from the cache if a new access token was required.

However, it does seem ok that I can manually get the refresh token from the cache using $client->getRefreshToken() and set it using $client->setRefreshToken($client->getRefreshToken()) and this will then allow the client to use the refresh token if it is required.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Weble/ZohoClient/issues/1#issuecomment-693253667, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAINRU7UTTWFBICEW7JJ3V3SGBYTFANCNFSM4OCCBPOA .

-- Daniele Rosario CTO

Weble.it http://weble.it/ Via Enrico Fermi 265 36100 Vicenza +39 0444 130094 <+390444130094> +39 328 3017134 <+393283017134>