Enrico Agrusti
Enrico Agrusti

Reputation: 514

Python - Download files from Google Drive as a scheduled task

yes I know similar questions have already been answered, but the fact that it has to work as a periodic job (from Windows task scheduler, every night at 21:00) is the point of the question.

I have a python script that downloads a single google spreadsheet as a .xlsx and saves it in a network folder. The script works when launched from command line, and it also works when executed from a .bat (which is the one that gets called in the scheduled task).

The task seems to work correctly except there is no file in the network folder. I suppose it's a matter of permission and authentication, although if run manually, after the first time it retains the authentication details and it works without any popup, so I don't understand why, through a scheduler, it should have problems with permissions.

I tried to run it also with the network administrator's account, but to no avail.

I repeat, the script on itself works correctly, it just does nothing (and does not return any error) when run through the scheduler. It's not the first python script that I schedule, but I still may be missing something.

Thanks


I'm adding the script, from which I removed the file ID and network path.

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import io

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.readonly']


def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('drive', 'v3', credentials=creds)

    # Call the Drive v3 API
    file_id = 'ID_OF_THE_SPREADSHEET'
    request = service.files().export_media(fileId=file_id,
                                           mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))
    with open("FILEPATH_OF_THE_NET_FOLDER", "wb") as f:  # Excel File
        f.write(fh.getbuffer())


if __name__ == '__main__':
    main()

Upvotes: 1

Views: 1286

Answers (1)

Enrico Agrusti
Enrico Agrusti

Reputation: 514

I answered myself, the problem was not about credentials, but rather about filepaths. Today I learned: using relative paths with scheduled tasks is not a good idea.

Upvotes: 1

Related Questions