3

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!

  • Not reproducible on my side, try to add [`includeItemsFromAllDrives=True`](https://developers.google.com/drive/api/guides/enable-shareddrives) to your query. – Emel Jun 08 '22 at 10:32
  • Try that, I got unexpected argument: Got an unexpected keyword argument includeItemsFromAllDrives – or.cohenadiv Jun 08 '22 at 11:29
  • Sorry my mistake, this parameter is for the list method. If you have access to the file you should have access to it, try using Try this API in the [`Files.get`](https://developers.google.com/drive/api/v3/reference/files/get) page, to check everything is working fine from there. Also, are you using a service account to check for the files? – Emel Jun 08 '22 at 13:05

2 Answers2

1

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:
Emel
  • 2,283
  • 1
  • 7
  • 18
0

To get the original file you must have access to the original file. Have them share that with you rather than a short cut.

404 File not found

Means exactly that the file does not exist, at least for you. In order to access private user data must have permission to access that file.

If someone gave you access to the shortcut. This does not mean that you have access to the file itself. You would have the same issue if they just gave you a share with link to a file. You don't have permissions on the file so you would not be able to access it via the api.

For you to have access you must have been granted permissions. It would show in a permssions.list for the file call.

Share a file with a user will grant permissions and they can use it in the api.

enter image description here

getting a share with me link will not grant the user permission to access the file. It wont insert a row in the permissions so the user will not have access via the api.

enter image description here

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