I am developing a Telegram bot utilizing aiogram3 in conjunction with the PyDrive library for Google Drive interactions. The bot successfully performs automatic authentication, and for the initial couple of hours, everything seems to work smoothly.
However, after a period of 2-3 hours, the bot unexpectedly ceases to function correctly. I am beginning to think this might be due to the expiration of authentication tokens that require a renewal, though I am not completely certain.
I have thoroughly reviewed the documentation for PyDrive and explored settings on the Google Cloud Platform, but unfortunately, I have not discovered a definitive solution yet. Has anyone encountered similar issues with authentication timeouts when running a long-lasting bot with PyDrive? What would be the best way to automatically refresh tokens?
Had this exact issue six months ago with a document management bot. It’s definitely token expiration - Google’s access tokens die after about an hour, and PyDrive sucks at auto-refresh for long-running apps. Fixed it by wrapping all PyDrive operations in try-catch blocks and forcing re-auth when credential errors pop up. I also store the refresh token separately and manually refresh it before API calls if the current token’s about to expire. The real issue is PyDrive’s token management wasn’t built for persistent apps like Telegram bots that run 24/7 for weeks.
yeah, totally get ur frustration. i had the same prob. i just created a cron job to refresh the tokens every 45 mins, works like a charm. also, make sure offline access is ticked in your credentials.json in Google Console, lotta folks skip that and get stuck.
PyDrive sucks at handling token refresh in background processes. Hit the same issue building a backup bot last year. What worked for me: add a token validation check before each Drive operation. I made a simple function that tests if credentials are still good with a lightweight API call - if it fails, trigger fresh auth. Also consider ditching PyDrive for Google Drive API v3 with google-auth libraries. Setup’s a bit trickier upfront, but refresh tokens work way better for bots that run 24/7.