Search code examples
pythongoogle-drive-apipydrive

Google Drive API Invalid Secrets File Through Task Scheduler


I need some help troubleshooting something that should be easy to figure out. I have the following files in this folder on my computer: C:\Users\theda\Documents\Python\CANSLIM Script

  • run_script.bat
  • client_secrets.json
  • credentials.json
  • settings.yaml

These same files (except for the .bat file) plus my Python code are in this folder: C:\Users\theda\PycharmProjects\CANSLIM_Script

.bat file code:

cmd /k python C:\Users\theda\PycharmProjects\CANSLIM_Script\canslim_script.py

When I run my script by double-clicking on run_script.bat, it is able to find my client_secrets.json file and authenticate me with Google Drive. However, when I run the same .bat file through Windows Task Scheduler, I am getting the following Traceback:

Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\oauth2client\clientsecrets.py", line 121, in _loadfile 
with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'

Where do I need to place my credentials/settings/etc. for PyDrive when running a Python script with a .bat file through Windows Task Scheduler? Or, how can I figure out where it is looking for the file?


Solution

  • I was able to get this to work by supplying a location to my auth and settings files using the following lines of code. Hope it helps someone:

    secrets_file = os.path.normpath("C:/Users/theda/Documents/Python/CANSLIM Script/client_secrets.json")
    settings_file = os.path.normpath("C:/Users/theda/Documents/Python/CANSLIM Script/settings.yaml")
    
    gauth = GoogleAuth(settings_file=settings_file)
    
    GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = secrets_file