谷歌表格 API 请求上的 CORS 授权

Ton*_*uth 4 xmlhttprequest google-sheets cors

我正在尝试通过表格 API 检索基于单元格的提要从谷歌电子表格中读取数据。该电子表格是私有的,所以我使用 oauth 2.0 来授权我的请求。

通过https://spreadsheets.google.com/feeds/spreadsheets检索有关我的驱动器帐户的基本信息工作正常,但是当我尝试通过 XMLHttp GET 请求直接访问我的电子表格中的数据到https://spreadsheets.google .com/feeds/cells

我收到“请求的资源上不存在‘Access-Control-Allow-Origin’标头”错误。

我已经设置了正确的授权令牌

 xhr.setRequestHeader('Authorization', 'Bearer ' + token);
Run Code Online (Sandbox Code Playgroud)

并尝试通过“激活”CORS

 xhr.setRequestHeader('Access-Control-Allow-Credentials', 'true');
Run Code Online (Sandbox Code Playgroud)

无济于事。

有关如何访问此资源的任何提示?

谢谢 !

Ada*_*m T 7

就在今天修复了这个非常相同的问题。而且我不需要启用/激活 CORS,因为我已经读到某些防火墙会为了安全而去掉标头。http://promincproductions.com/blog/server-proxy-for-cross-site-scripting-cors/

在 js 代码的全局部分,添加一个函数...

window.googleDocCallback = function () { return true; };
Run Code Online (Sandbox Code Playgroud)

然后,到您的 AJAX(假设是 GET?)请求中的 URL,如果您没有 URI 参数,请附加 ?callback=googleDocCallback

如果您确实有其他参数,请附加 &callback=googleDocCallback

有关更多信息,请参阅:https : //jvaneyck.wordpress.com/2014/01/07/cross-domain-requests-in-javascript/