Google Drive API 问题排查:Google 云 python 函数无法检测到共享文件夹中的 PDF 文件

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

我开发了一个在 Google Cloud Platform 上运行的 Python 脚本。该脚本利用 Google Drive API 和 Google Sheet API 访问 Google Drive 中属于某公司的文件夹,从该文件夹内的 PDF 文件中提取数据,然后将提取的数据传输到 Google Sheet。

为了确保功能正常,我设置了一个服务帐户并配置了必要的 API。此外,我还集成了一个秘密管理器,将该功能与 Google Drive 和 Google Sheet 链接起来。

我通过与服务帐户的电子邮件 ID 共享驱动器文件夹来授予对它们的访问权限。

但是,运行脚本后,Drive API 无法检测到共享文件夹中的 PDF 文件。令人惊讶的是,API 没有返回任何错误消息。

def list_files_in_folder(drive,folder_id):
  #print(folder_id)
  # List files in the specified folder
  query = f"parents = '{folder_id}'"
  files = []
  response = drive.files().list(q = query).execute()
  #print(f'response:{response}')
  files = response.get('files')
  #print(f'First page files: {files}')
  next_page_token = response.get('nextPageToken')

  while next_page_token:
    response = drive.files().list(q=query,nextPageToken=next_page_token).execute()
    files.extend(response.get('files'))
    next_page_token = response.get('nextPageToken')

  return files

为了解决该问题,我使用备用 Google 云端硬盘帐户测试了脚本,该帐户与公司原始驱动器主驱动器不同,后者由多个帐户访问。当我创建包含 PDF 文件的文件夹并使用相同的服务电子邮件 ID 共享它时,脚本成功访问了文件夹内容,没有任何错误。

python google-cloud-platform google-drive-api google-sheets-api
1个回答
0
投票

您的搜索查询有误

parents in '10krlloIS2i_2u_ewkdv3_1NqcpmWSL1w'

不等于

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.