Exporting a spreadsheet as text / csv using Drive v3 gives 500 internal errors

I tried to export a Google spreadsheet in csv format using the Google client library for Python:

 # OAuth and setups... req = g['service'].files().export_media(fileId=fileid, mimeType=MIMEType) fh = io.BytesIO() downloader = http.MediaIoBaseDownload(fh, req) # Other file IO handling... 

This works for MIMEType: application / pdf, MS Excel, etc.

According to Google documentation supported by text/csv . But when I try to make a request, the server gives 500 Internal Error .

Even using google's Drive API Playground , it gives the same error.

I tried:

As in v2, I added a field:

gid = 0

specify a worksheet for the request, but then this is a bad request.

+6
source share
2 answers

This is a known bug in Google code. https://code.google.com/a/google.com/p/apps-api-issues/issues/detail?id=4289

However, if you manually create your own request, you can download the entire file in bytes (media controls will not work).

With file as the identifier of the http file as the http object you allowed against, you can upload the file with

 from apiclient.http import HttpRequest def postproc(*args): return args[1] data = HttpRequest(http=http, postproc=postproc, uri='https://docs.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=csv' % file, headers={ }).execute() 

data Here is a byte object that contains your CSV. You can open something like:

 import io lines = io.TextIOWrapper(io.BytesIO(data), encoding='utf-8', errors='replace') for line in lines: #Do whatever 
+3
source

You just need to do Exponential Shutdown .

Take a look at this ExponentialBackOffPolicy documentation.

The idea is that the servers are temporarily unavailable and they should not be overloaded when they try to return.

The default implementation requires a return for status codes 500 and 503. Subclasses can be overridden if different status codes are required.

Here is a snippet of the Exponential Backoff implementation from the first link:

 ExponentialBackOff backoff = ExponentialBackOff.builder() .setInitialIntervalMillis(500) .setMaxElapsedTimeMillis(900000) .setMaxIntervalMillis(6000) .setMultiplier(1.5) .setRandomizationFactor(0.5) .build(); request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff)); 

You can see this documentation for the ExponentialBackoff summary.

+2
source

All Articles