Javascript 不是来自 Google API Oauth 的客户端的有效来源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44068680/
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
Not a valid origin for the client from Google API Oauth
提问by Filipe Ferminiano
I'm receiving this error from Google API Oauth:
我从 Google API Oauth 收到此错误:
idpiframe_initialization_failed", details: "Not a valid origin for the client: http://127.0.0.…itelist this origin for your project's client ID
idpiframe_initialization_failed", details: "不是客户端的有效来源:http://127.0.0....为您的项目的客户端 ID 列出此来源
I'm trying to send a request from this local path:
我正在尝试从此本地路径发送请求:
And I already added this URL to the Authorized JavaScript origins
section:

我已经将此 URL 添加到 Authorized JavaScript origins 部分:

This is my code:
这是我的代码:
<!-- The top of file index.html -->
<html itemscope itemtype="http://schema.org/Article">
<head>
<!-- BEGIN Pre-requisites -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
</script>
<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer>
</script>
<!-- END Pre-requisites -->
<!-- Continuing the <head> section -->
<script>
function start() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: 'MY CLIENT ID.apps.googleusercontent.com',
// Scopes to request in addition to 'profile' and 'email'
//scope: 'https://www.google.com/m8/feeds/'
});
});
}
</script>
</head>
<body>
<button id="signinButton">Sign in with Google</button>
<script>
$('#signinButton').click(function() {
// signInCallback defined in step 6.
auth2.grantOfflineAccess().then(signInCallback);
});
</script>
<!-- Last part of BODY element in file index.html -->
<script>
function signInCallback(authResult) {
if (authResult['code']) {
// Hide the sign-in button now that the user is authorized, for example:
$('#signinButton').attr('style', 'display: none');
// Send the code to the server
$.ajax({
type: 'POST',
url: 'http://example.com/storeauthcode',
// Always include an `X-Requested-With` header in every AJAX request,
// to protect against CSRF attacks.
headers: {
'X-Requested-With': 'XMLHttpRequest'
},
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
// Handle or verify the server response.
},
processData: false,
data: authResult['code']
});
} else {
// There was an error.
}
}
</script>
<!-- ... -->
</body>
</html>
How can I fix this?
我怎样才能解决这个问题?
采纳答案by user7191932
If it's all the same to you, try adding http://localhost:8887to your authorized JavaScript origins instead. Had that error myself at some point and this fixed it. Know that you will have to use this URL for your request as well event though it translates to http://127.0.0.1:8887/.
如果对您来说都是一样的,请尝试将http://localhost:8887 添加到您的授权 JavaScript 源中。我自己在某个时候遇到过这个错误,这修复了它。知道您必须将此 URL 用于您的请求以及事件,尽管它会转换为http://127.0.0.1:8887/。
回答by Andrew McOlash
I had a very similar issue to yours. I tried to add multiple whitelisted ports from localhostand nothing was working. Ended up deleting the credentials and setting them up again. Must have been a bug on googles end for my setup.
我有一个与你非常相似的问题。我尝试从其中添加多个列入白名单的端口localhost,但没有任何效果。最终删除凭据并重新设置它们。对于我的设置,一定是谷歌端的一个错误。
回答by Kirill Kovalevskiy
回答by vtellier
I read on several places on the web people use to redo the creation of the credentials to get it to work.
So I did, I created a new credential for the same project and used my new user-idand it worked perfectly... Looks like the edition of the whitelist is a bit flacky...
我在网上的几个地方阅读了人们用来重做凭据以使其工作的地方。
所以我做了,我为同一个项目创建了一个新的凭证,并使用了我的新用户 ID,它工作得很好......看起来白名单的版本有点不稳定......
Nb:I also used localhostinstead of 127.0.0.1, IPs are not valid.
注意:我也使用localhost而不是127.0.0.1,IP 无效。
回答by user5886944
I fiddled around for about 10 minutes and then it finally worked when I tried http://localhost/in the browser (instead of 127.0.0.1)
我摆弄了大约 10 分钟,然后当我在浏览器中尝试http://localhost/(而不是 127.0.0.1)时它终于起作用了
Added the url at every place you can do white-lists at: https://console.developers.google.com/apis/credentials/
在每个你可以做白名单的地方添加了网址:https: //console.developers.google.com/apis/credentials/
回答by mahemoff
"Not a valid origin for the client" seems to be over-used by Google's API, i.e. it's misleadingly used for authentication errors too.
“不是客户端的有效来源”似乎被 Google 的 API 过度使用,即它也被误导用于身份验证错误。
For people seeing the error, check that the credentials are correct.
对于看到错误的人,请检查凭据是否正确。
(This might explain why it works for some people after re-creating credentials - in some cases, the original credentials might not have been correct).
(这可能解释了为什么在重新创建凭据后它对某些人有效 - 在某些情况下,原始凭据可能不正确)。
回答by TomHinkle
I just went through all of these solutions before realizing I was putting in
在意识到我正在投入之前,我刚刚经历了所有这些解决方案
https://localhost:3000
https://本地主机:3000
and my dev server was serving up
我的开发服务器正在服务
http://localhost:3000
http://localhost:3000
Stupid, I know, but someone else will probably make the same mistake and perhaps this comment will help them :)
愚蠢,我知道,但其他人可能会犯同样的错误,也许这个评论会帮助他们:)
回答by Rex
What worked for us was adding a non-localhost domain to the authorized origins. My colleague had his localhost-domains working after adding a non-existing local domain, e.g. http://test-my-app.local.
对我们有用的是向授权来源添加一个非本地主机域。我的同事在添加了一个不存在的本地域(例如http://test-my-app.local.
回答by Philipedia
I had this same issue; but this is what worked for me:
我有同样的问题;但这对我有用:
- Open console.developers
- Open the project name
- Click on the credentials
- Under the "name", click on the "web client 1"
- Under the "URLs", add "http://localhost:3000"
- 打开console.developers
- 打开项目名称
- 点击凭证小号
- 在“名称”下,单击“ Web 客户端 1”
- 在“ URLs”下,添加“ http://localhost:3000”

