Search code examples
pythongoogle-drive-apiservice-accounts

Google Drive API v3 (googleapiclient.errors.HttpError 404 File not found)


I'm trying to upload a pdf file created on my computer with path output_pdfs/<pdf-name>.pdf. I'm using the id of the parent folder of where I want to upload the files to in my code. I'm getting an HttpError 404 "File Not Found .", referring to the parent ID (parent folder). I've read here I can get the parentID by using the childID but that doesn't work as I want to create a sub-folder (child) because it might not exist yet.

Steps I've done:

  1. Shared the Google Drive folder with the service account and set it to "Content Manager"
  2. Added auth scopes for my service account in my python code "https://www.googleapis.com/auth/drive"
  3. Ensured that my gcloud credentials are set correctly, gcloud auth list shows I'm using the same credentials that I shared the sheet with.
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()

Thank you.


Solution

  • So because the folder's in a shared drive, when listing parent folders you need to use includeItemsFromAllDrives=True and supportsAllDrives=True and use supportsAllDrives=True again when creating the file. I've tested this code which finds parent folders by name, creates a file and uploads it

    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