【问题标题】:Drive API 404 creating file inside a folderDrive API 404 在文件夹内创建文件
【发布时间】:2021-07-16 15:26:42
【问题描述】:

我正在尝试在我的驱动器上的文件夹中创建一个文件。

该文件夹存在于我的驱动器中,并与我正在使用的帐户共享(在代码中,我将使用假 ID:1AKIQHcwQVYgGcinp335Uu5C24kI1tJaq 引用它)

这是我创建文件的代码

from apiclient import errors
from apiclient.http import MediaFileUpload

def create_file(service, title, description, parent_id, mime_type, filename):

  media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
  body = {
          'name': title,
          'title': title,
          'description': description,
          'mimeType': mime_type,
          'parents': [parent_id]
          }
  try:
      file = service.files().create(
              body=body,
              media_body=media_body,
              fields='id').execute()
      return file
  except errors.HttpError as error:
      print('An error occurred: %s' % error)
      return None

但我总是收到此错误:

<HttpError 404 when requesting https://www.googleapis.com/upload/drive/v3/files?fields=id&alt=json&uploadType=resumable returned "File not found: 1AKIQHcwQVYgGcinp335Uu5C24kI1tJaq.">

我的代码基于this,有什么我遗漏的吗?文件夹位于 gsuite 共享驱动器内的问题是否存在?

【问题讨论】:

  • 确保您授权的用户有权访问该目录。
  • 如果用户有权访问该目录,您能否确认您的 parent_id 是否有效? 404 错误有时是由无效的 parent_id 引起的。某处可能有错别字。登录父 ID 并通过访问链接 https://drive.google.com/corp/drive/u/0/folders/ + parent_id 进行测试。
  • 谢谢,找到错误了。要使用共享驱动器中的文件夹,您必须提供 driveId

标签: python-3.x google-api google-drive-api drive


【解决方案1】:

我找到了解决办法!

要使用共享驱动器中的文件夹,您必须提供 driveId:

from apiclient import errors
from apiclient.http import MediaFileUpload

def create_file(service, title, description, parent_id, drive_id, mime_type, filename):

  media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=True)
  body = {
          'name': title,
          'title': title,
          'description': description,
          'mimeType': mime_type,
          'parents': [parent_id],
          'driveId': drive_id
          }
  try:
      file = service.files().create(
              body=body,
              media_body=media_body,
              fields='id').execute()
      return file
  except errors.HttpError as error:
      print('An error occurred: %s' % error)
      return None

【讨论】:

  • 我不喜欢显示的错误代码是它太笼统了。许多不同的问题都返回 404,您需要全部检查。很高兴你仍然能够自己解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多