1

Here is a quickpython script that I wrote to upload a file to Google Drive:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()           
drive = GoogleDrive(gauth)  

upload_file_list = [#placeholderfilename]
for upload_file in upload_file_list:
    gfile = drive.CreateFile({'parents': [{'id': '#placeholderdriveID'}]})
    # Read file and set it as the content of this instance.
    gfile.SetContentFile(upload_file)
    gfile.Upload() # Upload the file.

This script works, but every time it runs it also opens a window to authenticate the account accessing the drive. My goal for this script is to run it every morning with no input. Is there a way to set up the API so I don't need to manually authenticate the account every time I access google drive? Or does Google not allow that?

  • follow the steps in https://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention . You will need to figure out how to inject an Access Token to GoogleDrive, or simply throw the library away and call the https endpoints directly. – pinoyyid Feb 22 '23 at 15:44
  • or see https://stackoverflow.com/questions/24419188/automating-pydrive-verification-process?rq=1 – pinoyyid Feb 22 '23 at 15:48
  • The Answer to this question was that I needed to set up a service account through Google's Cloud Console. You'll need to download a JSON file with the account info. Put it in the working directory to allow the script to access it. Here's an article that details how to upload a file with a service account in JavaScript ( https://www.labnol.org/google-api-service-account-220404 ). Obviously the JavaScript Section isn't helpful for writing python scripts, but it will show you the steps to create a service account. – Patrick McGavick Mar 30 '23 at 17:28
  • This is a blog that helped me set up my own script: https://blog.zephyrok.com/google-drive-api-with-service-account-in-python/ – Patrick McGavick Mar 30 '23 at 17:29

0 Answers0