Javascript 运行 Google“Hello Analytics” API 教程时,如何避免此 X-Frame-Options SAMEORIGIN 错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28545465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I avoid this X-Frame-Options SAMEORIGIN error when running the Google "Hello Analytics" API tutorial?
提问by Alex Bowyer
I've been trying to run the https://developers.google.com/analytics/solutions/articles/hello-analytics-apitutorial to get up and running with pulling data programmatically from Google Analytics.
我一直在尝试运行https://developers.google.com/analytics/solutions/articles/hello-analytics-api教程,以便以编程方式从 Google Analytics 中提取数据。
I've copied the sample files exactly, but when I access them via localhost in Chrome, I get the following error in the JavaScript console, and get redirected to about:blank:
我已经完全复制了示例文件,但是当我通过 Chrome 中的 localhost 访问它们时,我在 JavaScript 控制台中收到以下错误,并被重定向到about:blank:
Refused to display 'https://accounts.google.com/o/oauth2/auth?client_id=363192057646-fbj7q1oais...%2Flocalhost&response_type=token&state=327475409%7C0.2024869166&authuser=0' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
I've tried all manner of tweaks but can't get this error to go away. Hope someone can assist (or indeed just direct me to a simple, working, Javascript example for accessing the Google Analytics API.
我尝试了各种调整,但无法消除此错误。希望有人可以提供帮助(或者实际上只是将我引导到一个简单的、有效的、用于访问 Google Analytics API 的 Javascript 示例。
回答by elliotrock
Please check the Authorized JavaScript originsurl in the Google API console in your Oauth Settings. This must be where you are authorising the javascript.
请 在您的 Oauth 设置中的 Google API 控制台中检查Authorized JavaScript originsurl。这必须是您授权 javascript 的地方。
回答by Gledsley Muller
I was having the same issue yesterday but then I realised I was using the wrong Client ID on my Credentials.
我昨天遇到了同样的问题,但后来我意识到我在我的凭据上使用了错误的客户端 ID。
You should double check if you created a 'Client ID for web application' on APIs & auth > Credentials. And then use that Client ID.
您应该仔细检查是否在APIs & auth > Credentials上创建了“Web 应用程序的客户端 ID” 。然后使用该客户端 ID。
I my case, I wrongly created a 'Service Account' first and used that Client ID. Then I realised the mistake and created a 'Web Application' and replaced the Client ID on hello_analytics_api_v3_auth.js (according to the tutorial on https://developers.google.com/analytics/solutions/articles/hello-analytics-api).
我的情况是,我首先错误地创建了一个“服务帐户”并使用了该客户 ID。然后我意识到错误并创建了一个“ Web 应用程序”并替换了 hello_analytics_api_v3_auth.js 上的客户端 ID(根据https://developers.google.com/analytics/solutions/articles/hello-analytics-api上的教程)。
Btw, don't forget to create a Public API Access key.
顺便说一句,不要忘记创建一个公共 API 访问密钥。
EDIT:if you are using the google examplefix the following function:
编辑:如果您使用谷歌示例修复以下功能:
function handleAuthResult(authResult) {
if (authResult) {
gapi.client.load('analytics', 'v3', handleAuthorized);
} else {
handleUnauthorized();
}
}
On the if statement, change to:
在 if 语句中,更改为:
if (authResult && !authResult.error)
So you would end up with:
所以你最终会得到:
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
gapi.client.load('analytics', 'v3', handleAuthorized);
} else {
handleUnauthorized();
}
}
My colleague found the bug and made a pull requestto fix it. I hope that sorts the issue now. It sorted for me ;-)
我的同事发现了这个错误,并提出了一个拉取请求来修复它。我希望现在可以解决问题。它为我排序;-)
回答by pasx
I was having the same issue with a Fusion table example I found online.
我在网上找到的 Fusion table 示例也遇到了同样的问题。
None of the answers I found online were useful at all but I finally solved the issue as follows:
我在网上找到的答案都没有用,但我最终解决了这个问题,如下所示:
Open the dev console in Chrome which shows the error and the url it is trying to access, open the Url in a new tab.
在 Chrome 中打开显示错误和它试图访问的 URL 的开发控制台,在新选项卡中打开 URL。
The page shows this:
该页面显示:
400. That's an error.
Error: invalid_request
Parameter not allowed for this message type: client_secret
So I edited the code:
所以我编辑了代码:
function auth(immediate) {
gapi.auth.authorize({
client_id: clientId,
<!--client_secret: clientSecret,-->
scope: scopes,
immediate: immediate
}, handleAuthResult);
}
Et voila (:
等等 (:
回答by Michael Biermann
I had this error message many times before. Most of the time I just did call Google API inconsistently. To figure out, click the URL in the dev. console. A new window opens and you get a message like this:
我之前多次收到此错误消息。大多数时候我只是不一致地调用 Google API。要弄清楚,请单击 dev 中的 URL。安慰。一个新窗口打开,您会收到如下消息:
400.
That's an error.
Error: invalid_scope
Some requested scopes were invalid ...
回答by esilver
I got it to work by clearning my cookies. --Solution posted in this similar issue here: Google+ API "400 (Bad Request)" and "Refused to display ... in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'." errors
我通过清除我的 cookie 让它工作。--在此类似问题中发布的解决方案:Google+ API“400(错误请求)”和“拒绝在框架中显示......因为它将'X-Frame-Options'设置为'SAMEORIGIN'。” 错误

