本文介绍了谷歌表格 API 请求上的 CORS 授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

通过https://spreadsheets.google.com/feeds/spreadsheets 工作正常,但是当我尝试直接通过电子表格访问数据时对 https://spreadsheets.google.com/feeds/cells

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

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

我已经通过

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

并尝试通过

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

无济于事.

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

Any tipps on how to access this resource ?

谢谢!

推荐答案

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

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/

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

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

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

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

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

如果你有其他参数,附加 &callback=googleDocCallback

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

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

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

这篇关于谷歌表格 API 请求上的 CORS 授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 11:26