I am using the Google Drive API to export Google Sheets and save them locally but I've encountered the following problem:
One of the sheets is not downloading properly. I'm not sure if it's because of its size or complexity but by using the proposed code in the documentation the process gets stuck for several minutes and them times out.
Below is the code indicated in the documentation with minor changes to my work flow
file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M' # Changed to my file id
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' # MS Excel
request = drive_service.files().export_media(fileId=file_id, mimeType=mimetype)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while not done:
status, done = downloader.next_chunk()
if done:
# process the downloaded file
Since I didn't need all the sheets, I tried another solution by requesting the export manually using a HTTP request and setting the gid=0
parameter on the URL to indicate the desired sheet.
Firstly I tried the simple get API call just to make sure it works, as follows:
curl https://www.googleapis.com/drive/v3/files/<my_file_id>?access_token=<my_access_token>
That worked fine and I got a response with the file details.
After that, I tried the export request below but I always get a TEMPORARY REDIRECT
response even though the request is the same as in the documentation.
curl https://www.googleapis.com/drive/v3/files/<my_file_id>/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet&access_token=<my_access_token>
<HTML>
<HEAD>
<TITLE>Temporary Redirect</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Temporary Redirect</H1>
The document has moved <A HREF="big_giant_huge_url">here</A>.
</BODY>
</HTML>
So my questions are:
- is there any way to specify the sheets to download using the official Python API or resolve the download problems?
- anyone knows the cause for the
Temporary Redirect
in my HTTP request? - is there another solution to download the sheets?
Thanks!