-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do Refresh Token results work, if done automatically within the API? #901
How do Refresh Token results work, if done automatically within the API? #901
Comments
As long as the If you're looking to save the access token somewhere, this is tricky, as it's not currently being saved back to the |
Yes just so there's no confusion to my question: I can see that the API is using the I haven't played around with the cache yet at all (I didn't even know there was one). I will do so now - however out of interest, when would the cache get updated with those new values? Immediately after the Just a little unfamiliar with the code. Also just wondering how everyone else manages this as it would appear as a common scenario? Am I doing it wrong? (Very common occurrence for me!!!) Thank you for getting back to me. |
Haha, no problem! No, you're not the only one to have this issue. In the downstream auth library, we just added the method The access token is retrieved when $service = new Google_Service_Drive($client);
$files = $service->files->list();
$token = $client->getCache()->get(sprintf('%s:%s', $clientId, implode(':', $scopes))); It's not super elegant, but it should work. |
Hi again, Ok so having looked at the cache object I can see that there is indeed a key that holds the new The key name is long and tricky (!!) using the clientId and scopes as you've shown above...however, there's only the new It feels like this might just be a feature in progress? Perhaps I should wait a while until this gets fleshed out a bit more with the Or have I read too much between the lines above and it's not really something you're planning on implementing...in which case, maybe I should put a request in. I'll have to read your etiquette rules too, don't want to appear rude and demanding :) Thank you. |
Just noticed this issue/request too..looks very similar. #300 |
something like this might work: class Google_Client
{
// ...
public function authorize(...)
{
// ...
$this->setLastCredentials($credentials);
}
public function getLastReceivedToken()
{
if ($credentials = $this->getLastCredentials()) {
return $credentials->getLastReceivedToken();
}
}
} It's the easiest way to solve this problem, but it's not very elegant. A callback would work, or a better |
Hi good day, I have a question, is it possible to modify the expiry of the access_token? or of the session itself? or at least when the session expires, redirect or create a status message. Sorry for barging in to this thread, it just so happen that I am having trouble with keeping the session for a longer time for the google api thanks |
@jonnywilliamson PTAL at #905 |
@bshaffer - Hey I'm only able to get looking at this today. Just about to check out the new code! Thanks! |
I've faced this same issue, and managed to get it working with this Laravel code:
The problem is because the token callback only returns the access token, the rest of the token is no longer valid, particularly Is there any way of getting the full token back? Currently it appears that I can only get a valid |
Probably I am missing something here but it was as simple as this when I tested some weeks ago:
According to my tests $client->getAccessToken() will return the new access token if it was refreshed internally. Is this not enough? Am I wrong? |
Its been a while since I looked at this...I'll be digging into it again in a few weeks. Perhaps it might be the case now that |
@gsi-delmo I believe the issue is upon refreshing the token, the refresh token itself is not being persisted (see #1121) |
This is very interesting. None of the examples in the documentation tackle the problem of persisting the refreshed token at all. Instead, they keep using the initial token, which should include the That can't be good for network traffic! So the examples given seem to work beyond the 3600 seconds of the initial token being issued, but they are not working in the way people think they are. When persisting the new access token, it needs to be provided in the same form as it was originally (access token, expiry time, refresh token etc.) since that is exactly what was persisted for the original authentication, is exactly what is given to the API client each time it is used, and is exactly where the API client gets the refresh token from when it needs it. So if it is being replaced in storage with a refreshed version, it needs to be complete enough, so that new one will work in another 3600 seconds when that expires. Note: I am not manually renewing the token when its time has expired. I just letting the API client do that for me silently, which it will do (presumably on getting a 401) only if the access token it is given includes the refresh token in its structure. Additional: The API client auto-refresh seems to be time-based. Given the persisted access token, the $client->setTokenCallback(function ($cacheKey, $accessToken) use ($client) {
// Persist the access token.
$_SESSION['token']['access_token'] = $accessToken;
// Without moving the created time on, the API client will refresh on every page load.
$_SESSION['token']['created'] = time();
}); Now the question is what happens on the edge case? If the stored timestamp says the token has not expired, but Google with a server 5mS ahead of my clock says it has expired. Does the API client still silently refresh the token, or does it raise an exception? Getting the timing right to test this would be near impossible, so I guess I'm going to have to dive into the code. Edit: ah yes, I just wind the |
Hi, I've no problem with understanding the refresh token, it's purpose, how to get it OR how to use it.
I am confused however about how the api uses it automatically when an access_token has expired. Here's a short example.
My Code:
access_token
and expiry time from database. Detects that token should be expired. (usesisAccessTokenExpired()
)access_token
. Both saved/updated in database.access_token
used to get data from google service.That's all fine.
However, I see in the code in
Google_Client
that the check to see if theaccess_token
is expired is already called when theauthorise
method is called AND if it is expired and the refresh token has been supplied, it will use the refresh token when preparing the credentials.This is where I get confused.
At no stage during the execution of the
Google_Client
code, can I find a place where the results of using the refresh_token can be 'intercepted' and thus, saved/updated to the users database.I'd love to be able to remove all the time checking and
isAccessTokenExpired()
code from MY code and let the API deal with it (as it currently is). However, I DO want to know, if the API uses the refresh_token, what is the newaccess_token
and when does it expire! I want to be able to update my database so that if I send a second request a few seconds later, it doesn't use the OLDaccess_token
like it currently does in my codebase.Can anyone shed somelight on what/how this works?
Many thanks.
The text was updated successfully, but these errors were encountered: