Skip to content Skip to sidebar Skip to footer

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 mor

Solution 1:

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:.

Post a Comment for "How To Handle The Maximun Export Limit Size File For Drive Api"