【问题标题】:GoogleDrive API files().list returns only one file for the whole organizationGoogle Drive API files().list 只为整个组织返回一个文件
【发布时间】:2019-12-26 23:42:28
【问题描述】:

我有以下代码从 GoogleDrive 获取整个组织的所有文件:

    def list_files(self):
        results = self._service_v3.files().list().execute()
        items = results.get('files', [])
        return items

问题是该方法只返回一个文件(Getting started.pdf

我的整个代码如下所示:

class GoogleDriveModule(object):
    def __init__(self, config):
        credentials = service_account.Credentials.from_service_account_file(
        config['credentials-file'], scopes=config['resource-scopes'])
        self._service_v3 = build('drive', 'v3', credentials=credentials)

        def list_files(self):
            results = self._service_v3.files().list().execute()
            items = results.get('files', [])
            return items

如何检索驱动器中的所有文件?

【问题讨论】:

  • 您使用的是服务帐户还是您的用户凭据?您的云端硬盘中还有更多未删除的文件?
  • @AndresDuarte 我正在使用服务帐户。我的驱动器中有一些文件

标签: google-drive-api


【解决方案1】:

您需要为要从中检索文件的目标用户委派凭据 [1]。您可以使用 google.oauth2 库 [2] 中服务帐户模块中的 with_subject 函数来执行此操作,例如:

from googleapiclient.discovery import build
import google.auth.transport.requests
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)

delegated_credentials = credentials.with_subject('targetUser@example.com')
delegated_credentials.refresh(google.auth.transport.requests.Request())

service = build('drive', 'v3', credentials=delegated_credentials)

[1]Node.js - Can't access my account from the Google Drive API

[2]https://google-auth.readthedocs.io/en/latest/reference/google.oauth2.html

【讨论】:

  • 如果我想要特定用户的文件,我认为你是对的。我需要整个组织的文件(我应该在问题中提到它)
  • 那么您还需要使用 Admin SDK API 列出每个user 电子邮件并模拟每个电子邮件以获取他们的文件。使用循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-01
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多