As per spec, Google Docs can be downloaded in different formats using the appropriate exportLinks
entry in the file metadata obtained using the Drive API. The URLs specified by these entries support CORS, so one can use them from pure Javascript, without a proxy.
This works for most files and document types (doc, presentation) but not spreadsheets. When you try an XHR GET on a spreadsheet exportLinks
URL from browser Javascript,
- The OPTIONS request returns a valid response, but it has an Expires header of
Fri, 01 Jan 1990 00:00:00 GMT
- The browser gives up at this point (probably due to the Expires)
- Doing a GET with the same Authorization, Referer, Origin headers using
curl
returns the spreadsheet alright, but the response headers don't contain anyAccess-Control-Allow-Origin
header, so it wouldn't have worked from the browser.
Other document types behave in the expected manner for OPTIONS and GET, returning correct Expires and CORS headers.
In fact, old spreadsheets - those with exportLink URLs which look like https://docs.google.com/feeds/download/spreadsheets/Export?key=XXX&exportFormat=xlsx
also work. The problem is confined to new sheets, whose exportLink URLs look like https://docs.google.com/spreadsheets/export?id=XXX&exportFormat=xlsx
.
I hit this issue with pigshell using the following steps:
- Visit pigshell using a desktop browser (Chrome/Firefox/Safari)
- Attach a Google account by clicking on the Google icon in the rightbar
In the pigshell CLI,
cd /gdrive/<[email protected]> ls gcat <docfilename> # Displays PDF version of doc gcat <spreadsheetfilename> # CORS failure, examine browser console/devtools gcat <spreadsheetcreatedyearsago> # Works