How to track file name modifications in Google Drive API?

I’m struggling to find a way to see the history of file name changes in Google Drive using their API. The web interface shows when someone renames a file, but I can’t seem to get this info programmatically.

I’ve tried using revisions.list, but it doesn’t show anything for title changes. changes.list gives me too much info about the whole Drive, and changes.watch is for real-time updates, not history.

Is there a specific API endpoint I’m overlooking? Or maybe a different approach to get this data? I really need to track these file name modifications for my project.

Here’s a simple code snippet of what I’ve tried so far:

def get_file_revisions(file_id):
    revisions = drive_service.revisions().list(fileId=file_id).execute()
    for revision in revisions['items']:
        print(revision['modifiedDate'], revision['lastModifyingUser']['displayName'])
    # This doesn't show title changes

Any ideas on how to get this working? Thanks in advance!

I’ve been working with the Google Drive API for a while now, and I can tell you that tracking file name changes isn’t straightforward. One approach that’s worked for me is using the drive.activity.query method. It’s part of the Drive Activity API, which is separate from the main Drive API.

This method allows you to retrieve a list of changes made to files, including renames. You’ll need to set up the Drive Activity API in your project first. Then, you can query for activities on a specific file or folder.

Here’s a rough idea of how to use it:

from googleapiclient.discovery import build

service = build('driveactivity', 'v2', credentials=creds)
results = service.activity().query(body={
    'itemName': 'items/fileId',
    'ancestorName': 'items/folderId',
}).execute()

for event in results.get('activities', []):
    if 'rename' in event['primaryActionDetail']:
        print(f"File renamed at {event['timestamp']}")

This should give you the rename history you’re looking for. It’s not perfect, but it’s the closest I’ve found to tracking file name changes programmatically.

I’ve encountered a similar issue in my work with the Google Drive API. Unfortunately, there’s no direct method to retrieve a comprehensive history of file name changes. However, I’ve found a workaround that might help.

Consider using the files.get method with the fields parameter set to include name, modifiedTime, and version. By periodically calling this method and storing the results, you can create your own log of file name changes over time.

Here’s a basic approach:

  1. Regularly fetch file metadata
  2. Compare the current name with the previously stored name
  3. If different, log the change with timestamp

This method requires more manual tracking on your end, but it’s reliable for monitoring file name modifications. Keep in mind it won’t provide historical data before you start logging.

hey there! have u tried the files.list method with the fields parameter set to include modifiedTime and name? you could compare the results over time to track name changes. might be a bit hacky, but could work. good luck with ur project!