I’m working on a Python script to automate video uploads to Google Drive. The main issue I’m facing is the need to manually refresh tokens. I’ve tried using PyAutoGUI to simulate clicks for token refresh, but the program seems to freeze when the refresh window opens.
The simulate_clicks function uses PyAutoGUI to click through the refresh process:
def simulate_clicks(drive_type):
time.sleep(2)
# Click email based on drive type
# Click continue buttons
# Close window
The simulate_clicks function works when tested separately, but the whole process doesn’t work as expected. Any ideas on how to fix this or approach it differently?
I’ve encountered similar issues with Google API authentication and found that a headless approach using the google-auth-oauthlib library simplifies the process. I created a flow object using Flow.from_client_secrets_file and obtained the initial credentials by running flow.run_local_server with port 0. After acquiring the credentials, I saved them using pickle and loaded them on later executions. I also refresh the credentials automatically when they expire. This method avoids the complexities of simulating UI interactions and provides a more reliable and maintainable solution.
Based on my experience, rather than trying to simulate clicks with PyAutoGUI, I switched to using the google-auth-oauthlib library, which has proven to be much more reliable. When I first faced token refresh issues, manually automating the window interactions was error-prone and inconsistent. Instead, I stored the credentials after the initial authorization and then reloaded them when needed by converting the credentials to a dictionary. Handling exceptions for expired tokens also made the process smoother. This method not only eliminates the reliance on the UI but also simplifies the overall workflow.
hey alexlee, been there myself. have u tried using selenium instead of pyautogui? it’s way more reliable for web automation. u can set up a webdriver to handle the token refresh process. just make sure to use explicit waits for elements to load. might solve ur freezing issue. good luck!