【问题标题】:Google Drive API v3 (googleapiclient.errors.HttpError 404 File not found)Google Drive API v3(googleapiclient.errors.HttpError 404 File not found)
【发布时间】:2021-05-20 14:13:17
【问题描述】:

我正在尝试上传在我的计算机上创建的路径为 output_pdfs/<pdf-name>.pdf 的 pdf 文件。我正在使用我想在代码中上传文件的父文件夹的id。我收到一个 HttpError 404 "File Not Found .",指的是父 ID(父文件夹)。我已阅读 here 我可以使用 childID 获取 parentID,但这不起作用,因为我想创建一个子文件夹(子文件夹),因为它可能还不存在。

我已经完成的步骤:

  1. 与服务帐户共享 Google Drive 文件夹并将其设置为“内容管理器”
  2. 在我的 python 代码"https://www.googleapis.com/auth/drive" 中为我的服务帐户添加了身份验证范围
  3. 确保我的 gcloud 凭据设置正确,gcloud auth list 显示我使用的凭据与我共享工作表的凭据相同。
def deliver_to_google_drive(output_pdf, creds, parentFolderID, folderToCreate):
    drive_service = build('drive', 'v3', credentials=creds)
    file_metadata = {
        'parents':[parentFolderID],
        'name': output_pdf
    }
    media = MediaFileUpload(output_pdf, mimetype='application/pdf',resumable=True)
    file = drive_service.files().create(body=file_metadata, """<-- 404 error here"""
                                        media_body=media,
                                        fields='id').execute() 
    print('File ID: %s' % file.get('id'))
    file.Upload()

谢谢。

【问题讨论】:

标签: python google-drive-api service-accounts


【解决方案1】:

因此,由于该文件夹位于共享驱动器中,因此在列出父文件夹时,您需要使用includeItemsFromAllDrives=TruesupportsAllDrives=True,并在创建文件时再次使用supportsAllDrives=True。我已经测试了这段代码,它按名称查找父文件夹,创建一个文件并上传它

def deliver_to_google_drive(output_pdf, creds, folderToStorePdfs):
    drive_service = build('drive', 'v3', credentials=creds)
    page_token = None
    while True:
        query = "mimeType = 'application/vnd.google-apps.folder' and name = '%s'" % folderToStorePdfs
        response = drive_service.files().list(q=query,
                                              spaces='drive',
                                              fields='nextPageToken, files(id, name)',
                                              includeItemsFromAllDrives=True,
                                              supportsAllDrives=True,
                                              pageToken=page_token).execute()
                                              #NB includeItemsFromAllDrives and supportsAllDrives needed for shared drives
        for folder in response.get('files', []):
            print('Found folder: %s (%s)' % (folder.get('name'), folder.get('id')))
            print(output_pdf)
            filename = output_pdf.split('/')[1]

            file_metadata = {
                'parents':[folder.get('id')],
                'name': filename
            }
            media = MediaFileUpload(output_pdf, mimetype='application/pdf',resumable=True)
            file = drive_service.files().create(body=file_metadata,
                                                media_body=media,
                                                fields='id',supportsAllDrives=True).execute()
            print('File ID: %s' % file.get('id'))
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多