python - 如何使用 Google Drive API 通过服务帐户访问域中的共享文件?

标签 python google-cloud-platform google-drive-api

我一直在尝试从 Python 3.7 脚本访问有关 Google Shared Drive 文件的一些简单信息:

The last time a Google Sheets file on a shared drive was modified.

我在 GCP Drive API 菜单中创建了一个服务帐户,它可以通过 Sheets API 毫无问题地访问/编辑/等 Google 表格。

但是,当我为 Drive API 使用相同的服务帐户时,它不会返回有关其自身文件夹(仅包含一个文件:“Getting Started”)之外的文件的任何信息。该帐户可以访问所有云 API,具有域范围的委派,并且在 GSuite 的 API 控制菜单中包含与 Drive API 相关的所有范围。

服务帐户的电子邮件地址已正确添加到共享驱动器中的所有文件夹。

有什么想法吗?基本上,我只需要知道任何给定用户最后一次修改工作表的时间。

secret_cred_file = ...
SCOPES = ['https://www.googleapis.com/auth/drive']
credentials = service_account.Credentials.from_service_account_file(secret_cred_file, scopes=SCOPES)
service = discovery.build('drive', 'v3', credentials=credentials)
results = service.files().list(pageSize=10, fields="nextPageToken, files(id, name,modifiedTime)").execute()
items = results.get('files', [])

PS:我看过这个:Getting files from shared folder但这并没有帮助

最佳答案

通过向 google documentation 中所述的列表方法添加一些参数,我能够在不模拟用户的情况下列出共享驱动器文件。 :

Implement shared drive support

Shared drives follow different organization, sharing, and ownership models from My Drive. If your app is going to create and manage files on shared drives, you must implement shared drive support in your app. To begin, you need to include the supportsAllDrives=true query parameter in your requests when your app performs these operations:

files.get, files.list, files.create, files.update, files.copy, files.delete, changes.list, changes.getStartPageToken, permissions.list, permissions.get, permissions.create, permissions.update, permissions.delete

Search for content on a shared drive

Use the files.list method to search for shared drives. This section covers shared drive-specific fields in the files.list method. To search for shared drive, refer to Search for files and folders.

The files.list method contains the following shared drive-specific fields and query modes:

driveId — ID of shared drive to search.

includeItemsFromAllDrives — Whether shared drive items should be included in results. If not present or set to false, then shared drive items are not returned.

corpora — Bodies of items (files/documents) to which the query applies. Supported bodies are user, domain, drive, and allDrives. Prefer user or drive to allDrives for efficiency.

supportsAllDrives — Whether the requesting application supports both My Drives and shared drives. If false, then shared drive items are not included in the response.

示例

service.files().list(includeItemsFromAllDrives=True, supportsAllDrives=True, pageSize=10, fields="nextPageToken, files(id, name,modifiedTime)").execute()

很高兴记住文件夹或文件需要与服务帐户共享。

关于python - 如何使用 Google Drive API 通过服务帐户访问域中的共享文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64396433/

相关文章:

python - 如何识别数据框列中的连续重复值?

python - Python 中 ProcessPoolExecutor 的运行调用次数不正确

python - 如何获取 PyObject 的引用计数?

php - Google Cloud API - 应用程序默认凭据

python - Google Cloud Composer 安装依赖项的时间太长

docker - GCP Kubernetes 工作负载 "Does not have minimum availability"

python - 在 pytest 中模拟模块导入

google-drive-api - 谷歌驱动器 API : Cannot copy a shared file

ios - RestKit Oauth 2 谷歌 API

google-drive-api - 来自 oauth2client 的 "AccessTokenRefreshError: Invalid Response 403"