Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google drive API V3 Fetch original file from shortcut

I have a Google Drive folder to which customers are uploading files. Some of them are actual GDrive files, and some are shortcuts to files on other drives or folders to which I have permission to read. I can successfully get the files using the Python Library. However, for the shortcut files I get a target id - but trying to get that ID ends up in a 404 error, even though I have permissions to access it on a web browser.

When I crawl my drive and use:

results = service.files().list(
            pageSize=10, fields="nextPageToken, files(id, name)").execute()
        items = results.get('files', [])

I get my entire drive, meaning every metadata about any file and folder there is on my drive. Some of theme are MimeType: 'application/vnd.google-apps.shortcut'. Those files have an addition field called: 'shortcutDetails'. It's look like this:

'shortcutDetails': {'targetId': SOME_ID, 'targetMimeType': SOME_MIMETYPE}

The targetId is supposed to point to the original file, but when I'm trying to get that file I get a bed request 404 file not find, I suppose because it's not in my drive. I am using this function:

def resolve_shortcut(service, target_id):
    logging.info(f"Resolving shortcut with target: {target_id}")
    results = service.files().get(fileId=target_id, fields="files(*)", supportsAllDrives=True).execute()
    raw_files = results.get("files", [])
    logging.info(f"Shortcut resolved, first result id: {raw_files[0]['id']}...")
    assert len(raw_files) == 1
    return raw_files[0]

And I get in return:

  <HttpError 404 when requesting
 https://www.googleapis.com/drive/v3/files/SOME_ID?fields=files%28%2A%29&supportsAllDrives=true&alt=json
 returned "File not found:
 SOME_ID.". Details: "[{'domain':
 'global', 'reason': 'notFound', 'message': 'File not found:
 SOME_ID.', 'locationType':
 'parameter', 'location': 'fileId'}]">

It there is any way to fetch the original file?? Thanks!

like image 554
or.cohenadiv Avatar asked Sep 11 '25 22:09

or.cohenadiv


1 Answers

The error 404 using the Files.get method refers that the users authenticated by the app have no access to that file.

Check list:

  • Login to the Drive SDK with the same account that you authenticated your application, and try to access that file.
  • Check that your application is using the right scope, ex: If you are using the https://www.googleapis.com/auth/drive.file you only have access to files that YOU have opened or created with the corresponding app https://www.googleapis.com/auth/drive
  • Refereed in the documentation:
    • Inform the user they don't have read access to the file or the file doesn't exist.
    • Instruct the user to contact the file's owner and request permission to the file.

Documentation:
  • Drive v3 Oauth
like image 167
Emel Avatar answered Sep 13 '25 12:09

Emel