3

I have a angularjs SPA.

It calls via XHR:

https://docs.google.com/spreadsheets/d/e/xxxxx-xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxx-/pub?gid=0&single=true&output=csv

which basically delivers a google sheet as CSV

since some days, i see a redirect, and there are no CORS headers any more. Before there have been CORS headers.

Has the API been changed? Is there some new API with CORS headers?

PS.: my workaround looks like this (on my ftp/php host):

<?php
$url = 'https://docs.google.com/spreadsheets/d/e/xxxxxxxxxxxxxx/pub?gid=0&single=true&output=csv';
$response = file_get_contents($url);
echo $response;
user1782357
  • 313
  • 2
  • 11

2 Answers2

2

You can use the Drive API method Files: Export

Using the Try this API will get you the link you have to send which is something like this:

GET https://www.googleapis.com/drive/v3/files/FILE ID/export?mimeType=text%2Fcsv&key=[YOUR_API_KEY] HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json

Kessy
  • 1,894
  • 1
  • 8
  • 15
1

As a workaround you can cache that csv locally on your server:

curl -s -L -o data.csv 'https://docs.google.com/spreadsheets/.../pub?...&output=csv'

Update e.g. every hour:

# /etc/crontab:
0 * * * * su -c '/home/your-user/path-to/cache-data.bash' your-user
sudo systemctl restart crond

And serve the cached csv data with e.g. nginx:

# nginx.conf:
server {
  # ...
  add_header Access-Control-Allow-Origin *;
  # ...
}
sudo sbin/nginx -s reload
Adobe
  • 12,967
  • 10
  • 85
  • 126