4

I'm trying to read data from a google spreadsheet by retrieving a cell based feed via the sheets API. The spreadsheet is private, so I'm using oauth 2.0 to authorize my requests.

Retrieving basic infos about my drive account via https://spreadsheets.google.com/feeds/spreadsheets works fine, but when I try to access the data from on of my spreadsheets directly via XMLHttp GET Request to https://spreadsheets.google.com/feeds/cells

I'm getting an "No 'Access-Control-Allow-Origin' header is present on the requested resource" error.

I've set the correct Authorization Token via

 xhr.setRequestHeader('Authorization', 'Bearer ' + token);

and tried to "activate" CORS via

 xhr.setRequestHeader('Access-Control-Allow-Credentials', 'true');

To no avail.

Any tipps on how to access this resource ?

Thanks !

Tonio Knuth
  • 41
  • 1
  • 1
  • 4

3 Answers3

7

Fixed this very same issue just today. And I did not need to enable/activate CORS as I've read that some firewalls will strip out the headers for security. http://promincproductions.com/blog/server-proxy-for-cross-site-scripting-cors/

In a global part of your js code, add in a function ...

window.googleDocCallback = function () { return true; };

Then, to the URL in your AJAX (GET assumed?) request, if you have no URI params, append ?callback=googleDocCallback

and if you do have other params, append &callback=googleDocCallback

For more info, please see: https://jvaneyck.wordpress.com/2014/01/07/cross-domain-requests-in-javascript/

Adam T
  • 675
  • 8
  • 22
  • Finally! I was trying to do this for hours and it was an error in my GS script that prevented the response by doing whatever it does that sends back a different sort of response when there's an error. But it works! – Vasily Hall Apr 21 '20 at 18:37
2

Google Sheets API has a known bug that doesn't support CORS for POST requests. Can you confirm that you are in fact making a GET request and not a POST/PUT/DELETE?

senornestor
  • 4,075
  • 2
  • 33
  • 33
  • Also, OPTIONS requests. A GET request that set headers changes automagically to OPTIONS request. Took me a while to figure this one out. – arve0 Nov 17 '15 at 23:55
1

You need to send a GET request. Make sure that there are no headers sent (not even content-type headers).

Lawrence Weru
  • 188
  • 1
  • 8