I can't access a shared file that is in a google shared drive

Viewed 1676

I am using Python to list out files shared with my google service account.

The process is working fine except that one file that I know is shared with my service account is not coming up in the output list.

The file is not owned by me so there is a limit to what I can show and trial, the one thing I have noticed is that the problem file "belongs to a shared drive" whereas the others do not. Also the "share" interface is very different to the one I am used to, I think again due to it being in a shared drive space.

Screenshots, the problem file share interface is all red/maroon unlike the normal ice blue/grey you get.

Shared drive file: share file interface problem file

Non shared drive file: share interface normal file

When I share the file with a "normal" google account it comes up in the drive UI in the shared with me area no issue.

My guess is there is some sort of hierarchy of permissions on the drive that is blocking my service account, hopefully though there is a simpler solution.

Things I've tried:

I have already asked the owner to add my service account email themselves, this did not change anything.

I have tried various string query changes like "sharedWithMe" and also looking in the appData location as well as drive.

If I share the file with a normal google user I can access it through the google drive "shared with me" area, so it is definitely "shared".

If I copy the file to my own area and share the file with the service account, the copy does appear in the output.

listing files code for reference

scope = 'https://www.googleapis.com/auth/drive.readonly'
api_name = 'drive'
api_version = 'v3'

##### I have a separate service builder function that is working fine

results = self.service.files().list(
        pageSize=1000, fields="nextPageToken, files(id, name, shared, 
        mimeType)").execute()
self.files = results.get('files', [])
if not self.files:
        print('No files found.')
else:
        print('Files:')
        for file in self.files:
            print(u'{0} ({1})'.format(file['name'], file['id']))

I expect the shared drive file to be listed in the output, but it is not. Other files are listing correctly.

1 Answers

In order to access the files on a shared Drive, you must specify the Drive ID in the files: list method using the a driveId=XXXXXXXXXXXXXXXXXX parameter.

You can first obtain this by using the Drives: list method provided by the Google Drive REST API as you can see here.

You can get a list of the files on a specific shared drive with the following code:

def getSharedDriveFiles(service):

    service = create_directory_service("Service_Account_Email")

    results = service.drives().list().execute()
    items = results.get('drives', [])

    driveIDs = []

    if not items:
        print('No files found')
    else:
        print('Drives:')
        for item in items:
            print(u'{0} ({1})'.format(item['name'], item['id']))
            driveIDs.append(item['id'])

    results = service.files().list(pageSize = 10, fields='nextPageToken, files(name, id)', driveId='SHARED_DRIVE_ID', corpora='drive', includeItemsFromAllDrives=True, supportsAllDrives=True).execute()

    items = results.get('files', [])
    if not items:
        print('No files found')
    else:
        print('Files:')
        for item in items:
            print(u'{0} ({1})'.format(item['name'], item['id']))

I made sure that my service account's email was added to the list of users the Drive is shared with and I was able to get an output of all the files in the Drive regardless of which domain the owner of the file is a member of.

Related