0

I started working with the Google Drive Changes API (https://developers.google.com/drive/api/v3/reference/changes). While my code can track all changes happening in My Drive, I cannot seem to filter out only specific changes like creation of new files. Whenever I'm uploading a file to a folder, there are 2 changes occurring, one which says about the change in file, and the other which says about the change in the folder. My code so far is as follows:

from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import schedule
import time

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

def job():
    print("Inside job()")
    global saved_start_page_token
    page_token = saved_start_page_token

    while page_token is not None:
        response = service.changes().list(pageToken=page_token, spaces='drive', restrictToMyDrive=True).execute()
        for change in response.get('changes'):
            # Process change
            file_id = change.get('fileId')
            print('Change found for file: %s' % file_id)
            print('Change type: %s' % change.get('changeType'))
            print('Filename: %s' % change.get('file').get('name'))
        if 'newStartPageToken' in response:
            # Last page, save this token for the next polling interval
            saved_start_page_token = response.get('newStartPageToken')
        page_token = response.get('nextPageToken')

def main():
    creds = None
    pickle_file = 'token.pickle'
    # 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(pickle_file):
        with open(pickle_file, '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(pickle_file, 'wb') as token:
            pickle.dump(creds, token)
    
    global service

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

    # Call the Drive v3 API
    response = service.changes().getStartPageToken().execute()

    global saved_start_page_token
    saved_start_page_token = response.get('startPageToken')

    schedule.every().minute.do(job)

    while True:
        schedule.run_pending()
        time.sleep(1)

if __name__ == '__main__':
    main()

Can I filter out changes to only show file/folder creation changes?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

0 Answers0