Search code examples
pythonpython-3.xgoogle-drive-apigoogle-drive-shared-drive

Cannot Interact with Shared Drives & Files | Python Google Drive API v3


I'm working on a python script that would manage a file on a shared drive using Google Drive API v3. However, when I try to download or replace that file, I get the following error:

An error occurred: <HttpError 404 when requesting https://www.googleapis.com/upload/drive/v3/files/1HhCxshcR17I4rM0gtBIvHzEH_jzd7nV2?alt=json&uploadType=multipart returned "File not found: 1HhCxshcR17I4rM0gtBIvHzEH_jzd7nV2.">

Here is my code

def update_file(service, file_id, new_title, new_description, new_mime_type,
                                new_filename):
    """Update an existing file's metadata and content.

    Args:
        service: Drive API service instance.
        file_id: ID of the file to update.
        new_title: New title for the file.
        new_description: New description for the file.
        new_mime_type: New MIME type for the file.
        new_filename: Filename of the new content to upload.
        new_revision: Whether or not to create a new revision for this file.
    Returns:
        Updated file metadata if successful, None otherwise.
    """
    try:
        # File metadata
        file_metadata = {
            'name': new_title, 
            'description': new_description
        }

        # File's new content.
        media_body = MediaFileUpload(
                new_filename, mimetype=new_mime_type) # In this case the file is small so no resumable flag needed

        # Send the request to the API.
        updated_file = service.files().update(
                fileId=file_id,
                body=file_metadata,
                media_body=media_body).execute()
        return updated_file
    except Exception as e:
        print ('An error occurred: %s' % e)
        return None

#Update an existing entry in database
def updateItem(dataIndex):
        
    userInput = input(header[2]) #Update quantity
    if userInput[0] == '+':
        ws['C' + str(dataIndex)] = str( int(ws['C' + str(dataIndex)].value) + int(userInput[1:]) )
    elif userInput[0] == '-':
        ws['C' + str(dataIndex)] = str( int(ws['C' + str(dataIndex)].value) - int(userInput[1:]) )
    else:
        ws['C' + str(dataIndex)] = str( userInput )
    
    ws['D' + str(dataIndex)] = today #Update date
    wb.save(filepath)

How do I use Google Drive API on a shared drive or document? This works fine in my personal drive.


Solution

  • If you want to use the file of file_id in your shared Drive with files().update() in Drive API v3, how about the following modification?

    Modified script:

    updated_file = service.files().update(
            fileId=file_id,
            body=file_metadata,
            media_body=media_body,
            supportsAllDrives=True  # <--- Added
            ).execute()
    
    • The default value of supportsAllDrives is false. So in this case, true is used for using the file in the shared Drive.

    Reference: