I am trying to download a bunch of files from a shared folder in Google Drive. While I am able to download some files, I get errors on different kinds of files. Note that, I am forcing the files to be exported to text/plain. Below is the code and three kinds of errors I have seen so far. I need help in downloading all the files in that folder preferably in plain-text format and if unable to convert, I want them to be downloaded as is. Any help/suggestions is appreciated. Please note that I am able to authenticate and let the code proceed after authentication. Some files are getting downloaded and fail when it hits some exceptions.
The errors pertain to these:
- Use Export with Docs Editors files
- The requested conversion is not supported.
Any suggestion to overcome these (and other) errors and download all files will be great! I have improved my code to look for certain known errors and moving on. This new code performs better in the sense that it has downloaded most of the files in Shared Google Drive. Still, I want a way to download all....
# Paginate file lists by specifying number of max results
for file_list in drive.ListFile({'q': 'trashed=false', 'maxResults': 500}):
print('Received %s files from Files.list()' % len(file_list)) # <= 10
for i, file1 in enumerate(file_list):
print('\ntitle: %s, id: %s' % (file1['title'], file1['id']))
print('Downloading {} from GDrive ({}/{})'.format(file1['title'], i, len(file_list)))
if file1['title'] in ['SOPs']:
continue
#file1.GetContentFile(file1['title'], mimetype="text/plain", remove_bom=True)
try:
#file1.GetContentFile(file1['title'])
file1.GetContentFile(file1['title'], mimetype="text/plain", remove_bom=True)
except errors.HttpError as err:
print(err)
continue
except ApiRequestError as err:
print(err)
continue
except:
print(f'Some other error')
continue
======================
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
from googleapiclient.errors import HttpError
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = '/Users/kk/acs/client_secrets.json'
def main():
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
for file_list in drive.ListFile({'q': 'trashed=false', 'maxResults': 500}):
print('Received %s files from Files.list()' % len(file_list)) # <= 10
for i, file1 in enumerate(file_list):
print('\ntitle: %s, id: %s' % (file1['title'], file1['id']))
print('Downloading {} from GDrive ({}/{})'.format(file1['title'], i, len(file_list)))
#file1.GetContentFile(file1['title'], mimetype="text/plain", remove_bom=True)
file1.GetContentFile(file1['title'])
# errors:
# pydrive2.files.ApiRequestError: <HttpError 400 when requesting https://www.googleapis.com/drive/v2/files/1k*********6UEZb7/export?mimeType=text%2Fplain&alt=media returned "Export only supports Docs Editors files.". Details: "[{'message': 'Export only supports Docs Editors files.', 'domain': 'global', 'reason': 'badRequest'}]">
# googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v2/files/1kd*********UEZb7?acknowledgeAbuse=false&alt=media returned "Only files with binary content can be downloaded. Use Export with Docs Editors files.". Details: "[{'message': 'Only files with binary content can be downloaded. Use Export with Docs Editors files.', 'domain': 'global', 'reason': 'fileNotDownloadable', 'location': 'alt', 'locationType': 'parameter'}]">
# pydrive2.files.ApiRequestError: <HttpError 400 when requesting https://www.googleapis.com/drive/v2/files/1s-*********2mOw/export?mimeType=text%2Fplain&alt=media returned "The requested conversion is not supported.". Details: "[{'message': 'The requested conversion is not supported.', 'domain': 'global', 'reason': 'badRequest', 'location': 'convertTo', 'locationType': 'parameter'}]">
if __name__ == '__main__':
main()