对Google表单API请求的CORS授权 [英] CORS authorization on google sheets API requests
问题描述
我试图通过sheet API检索基于单元格的Feed,从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 工作正常,但当我尝试通过
直接从我的电子表格中访问数据时XMLHttp GET请求到 https://spreadsheets.google.com/feeds/cells
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
我'在所请求的资源错误上存在NoAccess-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; };
然后,如果你没有URI参数,你的AJAX ,追加?callback = googleDocCallback
Then, to the URL in your AJAX (GET assumed?) request, if you have no URI params, append ?callback=googleDocCallback
,如果您有其他参数,
append & callback = googleDocCallback
and if you do have other params,
append &callback=googleDocCallback
有关详情,请参阅: https://jvaneyck.wordpress.com/2014/01/07/cross-domain-requests-in-javascript /
这篇关于对Google表单API请求的CORS授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!