-
Notifications
You must be signed in to change notification settings - Fork 168
Token expires after 1 hour #413
Comments
This sounds as though you did not enable refresh tokens when creating your secret. Google have changed things on this quite a few times so report back if any of the steps in that documentation linked don't look right. |
I wiped the old application and made a new app completely from scratch and followed each step exactly as instructed by the documentation. Nothing about the documentation was off, but the token still expires after exactly 1 hour. Here's what the {
"installed": {
"client_id": "xxx",
"project_id": "gphotos-380208",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "xxx",
"redirect_uris": [
"http://localhost"
]
}
} and here's what the {
"access_token": "xxx",
"refresh_token": "xxx",
"token_type": "Bearer",
"scope": [
"https://www.googleapis.com/auth/photoslibrary.readonly",
"https://www.googleapis.com/auth/photoslibrary.sharing"
],
"expires_at": 1678458526.649647
} here's a screenshot of the app as configured on google: |
Closing as stale. Please re-open if you still want to follow up. |
I'm having this issue as well. There doesn't appear to be a "refresh" setting that I can see when creating the client_secret. I just configured gphotos-sync (locally to get the token), and then the container on a Synology. The token expired after an hour. I went through the client_secret setup again, but there doesn't appears to be a refresh setting. The consent screen shows "Verification not required" and "In production". Since I didn't create the issue, I can't re-open it, would you prefer a file a new one? |
I went through the flow for a 3rd time, and I'm getting the same issue. Token expires after 1 hour of running as a container. |
Can confirm i was also unable to find a solution to this problem, and I'm aware that that the problem itself is not an issue related to the actual working script and something to do with what's happening on the Google end of things. |
I'm digging a bit through the authorization flow and the refresh via OAuth2Session. Reading the docs, it states that it's expected for the token to have an "expires_in" property. Interestingly, the token Google is handing out does NOT have an expires_in property (only expires_at). Could this be the problem? Maybe this is causing the token not to be refreshed.
I also fixed a time zone mismatch between the system from where I was getting the token and the container (by setting the TZ environment variable), but this did not resolve the issue, and the token still becomes invalid after exactly 1 hour. @deathtenk Could you re-open this issue? |
Reopening. I would like to follow up on the question of configuring a consent screen. I have a strong recollection that this was reported as a cuase of token expiry but I can't find the issue right now. |
Thanks @IslandJohn. I'm going to walk through the setup again this weekend and see if I can reproduce the error - will be using your screens as a comparison. |
I see now in OAuth lib that expires_at is sufficient and expires_in is actually converted to that property, and compared for expiry all in UTC. I got a new token with --new-token at 17:00 EDT (21:00 UTC), which came with an expire_at of 1680141610. This is 5 hours from the time of the request, which seems suspicious because it's 1 more than the UTC/TZ difference of 4 hours I'm in. If the tokens are expiring in an hour, the expire_at should have been closer to 1680127200 (18:00EDT/22:00UTC). UPDATE: I set the TZ environment variable on the system where I'm requesting the token to UTC ... Guess what? I got a token with the right expiry time of 1 hour. I made the request at 21:23 UTC and got the expire time of 1680128624 which is 22:23 UTC. So this seems to be an issue with time zone conversion somewhere. I'll test to see if this token will refresh (my guess is that it will - and my guess is also that the other token with the incorrect expire_at would also "start" working again after the expiry time since a refresh would be requested). UPDATE: The token I retrieved using a TZ=UTC worked and is not timing out after the 1 hour period. They continue to be refreshed appropriately. So this looks like a WORKAROUND: Make sure to "export TZ=UTC" before retrieving you first token. I have also reverted the TZ of the container to UTC. I haven't dug or thought too deeply, but perhaps the issue is in the Google libraries with the stripping of the time zone:
I don't have time right now to keep investigating more, but I filed a bug against google-auth here. |
@IslandJohn great work, thanks for that. I'm probably hampered by living in the UK and having my timezone = UTC for half the year - maybe I just lucked out with my tokens as a result. |
@IslandJohn wow what a bizarre issue. I'm gonna try this and see if the token refreshes correctly on my end. |
Leaving this open to monitor the state of googleapis/google-auth-library-python#1264 (thanks @IslandJohn !) Looks from the tags that Google are taking it seriously. |
Hey, I just noticed your issue here because I was dealing with another timezone issue the google auth library. I already posted a conclusion on the issue linked above, but let me add the conclusion also here, because I think the bug is in this repo. The
One problem is that I haven't found any documentation stating that it's in UTC, so all users of the library would have to read its code to figure it out. The bug happens in your code when you call |
@juzna thanks for this! I'm not totally clear what the fix is though. Do I need to copy flow.credentials.expiry.timestamp() into a variable and coerce its timezone before putting it in the token? |
So I drafted a PR where I localized the timestamp before I put it in the token. I don't know if I got things exactly correct (particularly where I account for DST) but from my understanding of the posts above this is what needs to happen. I'm open to any feedback. |
nvm, it was incorrect. I misunderstood the issue. still thinking through how to handle this pickle. |
Basically with a better understanding of the problem I was able to correct it by grabbing the system offset from |
The PR #435 was merged a while back and is now in release 3.2.0 |
When do you plan to push an update to Docker Hub? |
That is supposed to happen automatically, but has not. It looks like my merge of upstream skeleton has broken a few things even though the tests all pass. Will get back to you on this, but maybe not until the weekend. |
Hello, first of all thanks for all the hard work you put into this project. That said I've run into a wall, and even after parsing issues that appear to be relevant (this issue more specifically) I have not been able to fix this problem.
Setup: So to begin let me briefly explain what I've setup. I have a task running the gphotos-sync on a headless server. It runs every 5 minutes as a CRON task. I have an app on Google setup as per the instructions and it's published (it shows as Status: In Production).
Command: the command I'm running is the following:
Behavior: After exactly 1 hour I see the following error in the logs:
In order to fix this, I need to manually create a new
.gphotos.token
on the desktop and upload it to the server. This does allow it to continue as normal for another hour, however once the hour has elapsed the task fails with the 401 seen above. This doesn't seem very practical for what's supposed to be a hands-off solution for this, so what I'm wondering is if I'm missing something about token generation on googles end that would prevent the token from expiring like this?Another thing to note, I was looking at that .gphotos.token
expires-at
field, and that appears to show a datetime that's 7 hours after generation, not 1 hour. So I'm wondering what's causing the token to expire as early as it does.Am I at the mercy of Google here or is there anything I can do to fix this? Thanks for any help you can provide!
The text was updated successfully, but these errors were encountered: