我无法访问Google共享驱动器中的共享文件

时间:2019-07-03 10:13:05

标签: python google-api google-drive-api

我正在使用Python列出与我的Google服务帐户共享的文件。

该过程运行正常,但输出列表中未显示我与服务帐户共享的一个文件。

该文件不是我所有的,因此我可以显示和试用的文件受到限制,我注意到的一件事是问题文件“属于共享驱动器”,而其他文件则没有。另外,“共享”界面与我以前使用的界面非常不同,我想再说一次,因为它位于共享驱动器空间中。

屏幕截图,问题文件共享界面全为红色/栗色,与您得到的普通冰蓝色/灰色不同。

共享驱动器文件:共享文件界面 problem file

非共享驱动器文件:共享接口 normal file

当我使用“普通” Google帐户共享文件时,它会出现在“与我共享”区域的驱动器UI中。

我的猜测是,驱动器上存在某种权限层次结构,该层次结构阻止了我的服务帐户,希望有一个更简单的解决方案。

我尝试过的事情:

我已经要求所有者自己添加我的服务帐户电子邮件,但这没有任何改变。

我尝试了各种字符串查询更改,例如“ sharedWithMe”,并且还查找了appData位置以及驱动器。

如果我与普通的Google用户共享文件,则可以通过Google驱动器“与我共享”区域访问该文件,因此它肯定是“共享的”。

如果我将文件复制到自己的区域并与服务帐户共享文件,则副本确实会出现在输出中。

列出文件代码以供参考

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']))

我希望共享驱动器文件会在输出中列出,但事实并非如此。其他文件正确列出。

1 个答案:

答案 0 :(得分:0)

为了访问共享驱动器上的文件,必须使用driveId=XXXXXXXXXXXXXXXXXX参数在files: list方法中指定驱动器ID。

您首先可以使用Google Drive REST API as you can see here提供的Drives: list方法来获得此证书。

您可以使用以下代码获取特定共享驱动器上的文件列表:

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']))

我确保将我的服务帐户的电子邮件添加到共享该云端硬盘的用户列表中,并且无论该文件所有者是哪个域的成员,我都能获得该云端硬盘中所有文件的输出的。

相关问题