[GoogleDrive API files()。list仅返回一个文件

问题描述 投票:0回答:1

我有以下代码从GoogleDrive获取所有文件:

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

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

我的整个代码如下:

class GoogleDriveModule(BasePluginModule):
    def __init__(self, config):
        super().__init__(config)
        http = AuthorizeHttp(scopes=config['resource-scopes'],
                             client_email=config['delegating-user'],
                             creds_file=os.path.realpath(config['credentials-file'])).http
        self._service_v2 = discovery.build('drive', 'v2', http=http)
        self._service_v3 = discovery.build('drive', 'v3', http=http)

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

如何获取驱动器中的所有文件?

google-drive-api
1个回答
0
投票

您需要为要从中检索文件的目标用户委派凭据[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('[email protected]')
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

© www.soinside.com 2019 - 2024. All rights reserved.