google-api-python-client icon indicating copy to clipboard operation
google-api-python-client copied to clipboard

Downloading Excel file with Google Drive API doesn't bring lastest modification/version

Open mau21mau opened this issue 1 year ago • 1 comments

Summary

When I use Google Drive API to download my Excel files, it will send and outdated version. The way to reproduce this is to open the Excel file in Google Sheets Editor, than add new tabs, then download the File through the API. Most of the time it will not send the file with the latest added Tab.

Expected Behavior

Always when a change is applied to the file, downloading it should return that change

Steps to Reproduce the Problem

  1. Open the Excel file in Google Sheets Editor
  2. Add new tabs
  3. Then download the File through the API
  4. Most of the time it will not send the file with the latest added Tab

Specifications

  • Python version (Python 3.8.12)
  • OS (Ubuntu 22.04)

I'm having this issue using the Google build('drive', 'v3', credentials=creds) in my project, but could also reproduce this with a simpler script that I got here. It uses requests and it's way simpler:

Sample code:

import os
import requests

def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download&confirm=1"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)    

def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value

    return None

def save_response_content(response, destination):
    CHUNK_SIZE = 32768

    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk: # filter out keep-alive new chunks
                f.write(chunk)

if __name__ == "__main__":
    file_id = '1JI17N-NFAIOxX_2Y88gmuKMlsuGhBPcB'
    home = os.getenv('HOME')
    destination = f'{home}/Downloads/test-drive-download/output.xlsx'
    download_file_from_google_drive(file_id, destination)

Sample file: https://docs.google.com/spreadsheets/d/1JI17N-NFAIOxX_2Y88gmuKMlsuGhBPcB/edit#gid=1677858863 Demo: https://drive.google.com/file/d/1-L_SnWp1zQNWJ34Z2JtVbgnPVIONuBgr/view?usp=sharing

mau21mau avatar May 23 '23 23:05 mau21mau

+1 Also, I don't think this is Python client related at all. I have the same issue with golang library. Eventually the latest version will be downloaded, but i could not find any indication regarding this "eventual" time, nor could not I find any indication what file version is being downloaded

vladimirsavenkov avatar Aug 09 '23 16:08 vladimirsavenkov