Automated file upload to shared Google Drive without repeated authentication

I’m working on a project where I need to automatically send files to someone else’s Google Drive folder. The problem is that every time my script runs, it prompts for login credentials or verification codes.

Currently I’m using pydrive library but it’s really annoying having to authenticate manually each time. I want to set this up once and then let it run automatically without any user interaction.

Is there a way to store some kind of token or API key that would let me skip the login process after the initial setup? Maybe there’s a different library or approach that handles this better?

I’ve been searching for solutions but most examples I find still require manual authentication steps. What’s the best practice for this kind of automated file transfer scenario?

Had this exact headache with a backup automation project last year. Here’s what worked: combine service account authentication with proper folder sharing permissions. After creating the service account in Google Cloud Console, share the target Drive folder with the service account’s email address - this trips up most people. The folder owner has to explicitly grant access to your service account email, or you’ll get permission errors even with valid credentials. Then use the google-api-python-client library with the service account JSON file for completely headless operation. Been running automated uploads for 8 months now without a single authentication prompt. Just keep that JSON credentials file secure since it gives full access to whatever folders you’ve shared with the service account.

Yeah, you can fix this with proper token refresh handling. Set up OAuth2 credentials that auto-refresh without bugging the user. When you first authenticate, you get both an access token and refresh token. Store that refresh token securely - it won’t expire and lets you generate new access tokens automatically. I ran into this exact problem with a daily backup script. Once I switched from manual auth to storing refresh tokens in a secure config file, it’s been running for months without any prompts. Also, use the google-auth library instead of pydrive - handles token refresh way better in my experience.

service account creds are great for this! just go to google cloud console, set up a service account, and grab the json key file. after that, no more logins needed. it’s really cool for automating stuff!