Unable to retrieve files from Google Drive using Python API - Download fails

I’m working on a Python script to automatically fetch files from Google Drive using their API. I got the basic authentication working fine following the official guide, but now I’m stuck on the actual file download part.

Here’s my current code that’s giving me trouble:

from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
from oauth2client import file, client, tools
from httplib2 import Http
import io

API_SCOPE = 'https://www.googleapis.com/auth/drive.readonly'

def download_file():
    token_store = file.Storage('my_token.json')
    credentials = token_store.get()
    if not credentials or credentials.invalid:
        auth_flow = client.flow_from_clientsecrets('client_secrets.json', API_SCOPE)
        credentials = tools.run_flow(auth_flow, token_store)
    
    drive_api = build('drive', 'v3', http=credentials.authorize(Http()))
    
    target_file_id = 'my_document_id_here'
    download_request = drive_api.files().get_media(fileId=target_file_id)
    
    buffer = io.BytesIO()
    media_downloader = MediaIoBaseDownload(buffer, download_request)
    
    completed = False
    while not completed:
        progress, completed = media_downloader.next_chunk()
        print(f"Downloaded {int(progress.progress() * 100)}%")
    
    with open('saved_file.pdf', 'wb') as output_file:
        output_file.write(buffer.getvalue())

if __name__ == '__main__':
    download_file()

The error I keep getting is about MediaIoBaseDownload not being recognized. I tried different approaches but nothing seems to work. What am I missing here? Any help would be great because I’ve been stuck on this for hours.

looks like you might have an import issue there. try from googleapiclient.http import MediaIoBaseDownload again but make sure your google-api-python-client is updated. i had same problem last week and pip install --upgrade google-api-python-client fixed it for me. also double check the file id is correct

I ran into something similar when working with Google Sheets downloads. The issue might be with oauth2client library which is deprecated now. Try switching to google-auth and google-auth-oauthlib instead. Your authentication flow should use google.auth.transport.requests and google_auth_oauthlib.flow. Also worth checking if the file you’re trying to download actually supports the get_media() method - some Google Workspace files like Docs or Sheets need to be exported with a specific MIME type using files().export() rather than get_media(). The MediaIoBaseDownload import should work fine once you sort out the auth libraries.

Had a very similar frustration about two months ago when building a backup script. The MediaIoBaseDownload error usually happens when there’s a version mismatch between your installed packages. What worked for me was completely removing the old google packages first with pip uninstall google-api-python-client googleapiclient, then doing a fresh install. Another thing that caught me off guard was that some file types in Drive don’t actually have downloadable media content and will throw this error even with correct imports. Try testing your code with a simple image or PDF file first before attempting more complex document types. Also make sure your file ID is from a file you actually have download permissions for, because the API sometimes gives cryptic errors for permission issues.