Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
847 views
in Technique[技术] by (71.8m points)

xmlhttprequest - CORS authorization on google sheets API requests

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 !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...