user14398375
user14398375

Reputation: 175

How to handle the maximun export limit size file for drive api

I am trying to download some google doc files but after it i need to use the export method to convert into the microsoft word mimetype, it works fine until it found a file with more than 10 mb size, the api documentation said this is the limit size to export a document but i really need to download those files, everything in my script works fine except this the error that is throwing is

"This file is too large to be exported.". Details: "This file is too large to be exported." so , is there anyway to avoid this limitation or to export the document inside the folder that is content

EDIT: the document that i am trying to download is not public so i think i need to auth the request to get the content

EDIT 2 : script:


SCOPES = ['https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets']


  
def main():
    
   
    #----------------------Google drive auth-----------------------------
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    # Call the Drive v3 API
    service = build('drive', 'v3', credentials=creds)
    sheets_service = build('sheets', 'v4', credentials=creds)

    # Call the Sheets API
    sheet = sheets_service.spreadsheets()
    
   
    
    # ID of folder that contain the wanted files
    query = "'[ID OF THE FOLDER]' in parents"

    response = service.files().list(q=query,
                                spaces='drive',
                                fields='files(id, name, parents, webViewLink,exportLinks)').execute()
    
    baseURL="https://docs.google.com/document/d/"
    for document in response['files']:
        
        
        
        downloadURL=baseURL+document["id"]+"/export?format=doc"
        

            
        r = requests.get(downloadURL)  
        
        with open('pathtosabe, 'wb') as f:

            f.write(r.content)
            
   
  
        

            
        
main()

Upvotes: 0

Views: 1009

Answers (1)

Tanaike
Tanaike

Reputation: 201378

From your following replying,

well, that is the problem i don´t know how to use the acces token in the request the file is downloaded but the content is shown as corrupted i tryed with a public document and the content was visible

I thought that when your Google Document is not publicly shared, when the access token is used for your script of r = requests.get(downloadURL), it might work. So in this answer, I would like to propose the modified script using the access token retrieved from the authorization script of your script.

Modified script:

creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
    with open('token.pickle', 'rb') as token:
        creds = pickle.load(token)

if not creds or not creds.valid:
    if creds and creds.expired and creds.refresh_token:
        creds.refresh(Request())
    else:
        flow = InstalledAppFlow.from_client_secrets_file(
            'credentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

# Call the Drive v3 API
service = build('drive', 'v3', credentials=creds)
sheets_service = build('sheets', 'v4', credentials=creds)

# Call the Sheets API
sheet = sheets_service.spreadsheets()

# ID of folder that contain the wanted files
query = "'[ID OF THE FOLDER]' in parents"
response = service.files().list(q=query,
                            spaces='drive',
                            fields='files(id, name, parents, webViewLink,exportLinks)').execute()

access_token = creds.token # Added
baseURL="https://docs.google.com/document/d/"
for document in response['files']:
    downloadURL=baseURL+document["id"]+"/export?format=doc"
    r = requests.get(downloadURL, headers={'Authorization': 'Bearer ' + access_token})  # Modified
    with open('pathtosabe', 'wb') as f:  # Modified
        f.write(r.content)
  • In your script, 'pathtosabe, of with open('pathtosabe, 'wb') as f: is not enclosed by the single quote. Please be careful this. If you want to use pathtosabe as a variable, please declare it and modify to with open(pathtosabe, 'wb') as f:.

Upvotes: 3

Related Questions