Download a tab of a google sheet as a CSV file
This used to work:
curl 'https://docs.google.com/spreadsheets/d/1xaGu5Cd0kRPKy0v-EaVxgfnUxaxWlgDhfhfgRQy6hrc/gviz/tq?tqx=out:csv&sheet=Inv_Summary&range=A:G'
The segment tqx=out:csv specified the format, sheet specfied the tab name, and range the columns to bring out.
Now if I do this, I get:
<HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="https://accounts.google.com/ServiceLogin? service=wise&passive=1209600&continue=https://docs.google.com/spreadsheets/d/1_fZoLgNGB3fXZqgzCr1gtDnJ2Gowi4dFgovmjsSPHy8/gviz/tq?tqx%3Dout:csv%26sheet%3DInv_Summary%26range%3DA:G&followup=https://docs.google.com/spreadsheets/d/1_fZoLgNGB3fXZqgzCr1gtDnJ2Gowi4dFgovmjsSPHy8/gviz/tq?tqx%3Dout:csv%26sheet%3DInv_Summary%26range%3DA:G&ltmpl=sheets">here</A>.
</BODY>
</HTML>
That long response, broken to make it more easily parsible by people
<A HREF="https://accounts.google.com/ServiceLogin?
service=wise&passive=1209600&
continue=
https://docs.google.com/spreadsheets/d/1_fZoLgNGB3fXZqgzCr1gtDnJ2Gowi4dFgovmjsSPHy8/
gviz/tqtqx%3Dout:csv%26sheet%3DInv_Summary%26range%3DA:G&
followup=
https://docs.google.com/spreadsheets/d/1_fZoLgNGB3fXZqgzCr1gtDnJ2Gowi4dFgovmjsSPHy8/
gviz/tq?tqx%3Dout:csv%26sheet%3DInv_Summary%26range%3DA:G&
ltmpl=sheets">here</A>
You'll note that the followup URL is identical to the temp moved URL.
Now if I paste the original link after the curl statement into the address line in chrome, I get an immediate open a file to download, and it does the right thing.
Why isn't curl working to do this?
How do I get the old behaviour back?
Background: I keep a google sheet that tracks my tree farm inventory. The inventory is summarized on a pivot table. A script scrapes the pivot table data, and uses it to generate HTML pages for my website.
Gotta love informative messages.
- The spreadsheet in question was not publically readable.
- When google checked the file, curl did not pass an OAuth token. Instead of saying something clever like, "This app did not present a valid token" it instead refers me to the web page version.
- Since I was already authorized in the browser, the here link worked.
to solve this problem you can do one of two things:
A: Make the file publically readable -- anyone with the link can read it. Since this is basically just book keeping stuff in inventory control, I don't care who reads it.
B: Jump through the hoops to get OAuth working with Curl. A good starting point for that is here:
https://www.daimto.com/how-to-get-a-google-access-token-with-curl/
It's been answered before on SO
get google Oauth2 access token using ONLY curl
I remember an issue pertaining to output csv having a redirect for curl
commands, can you try a wget
with the same url? The issue comes and goes so I'll recommend a wget
if it's acceptable.
Sample:
wget -O test.csv --no-check-certificate "https://docs.google.com/spreadsheets/d/1xaGu5Cd0kRPKy0v-EaVxgfnUxaxWlgDhfhfgRQy6hrc/gviz/tq?tqx=out:csv&sheet=Inv_Summary&range=A:G"