简体   繁体   English

Google登录无法正常运行

[英]Google Sign-in is not working

I tried to use Google Sign-in for my website, however, it keeps giving me 400 error. 我尝试对我的网站使用Google登录,但是,它始终提示我400错误。

I referenced this article: https://developers.google.com/identity/sign-in/web/sign-in , and my code is really simple: 我引用了这篇文章: https : //developers.google.com/identity/sign-in/web/sign-in ,我的代码非常简单:

<html>
<head>
  <title>Test Google Login</title>
  <script src="https://apis.google.com/js/platform.js" async defer></script>
  <meta name="google-signin-client_id" content="<CHANGE IT>">
  <script>
  function onSignIn(googleUser) {
    var profile = googleUser.getBasicProfile();
    console.log('ID: ' + profile.getId());
    console.log('Name: ' + profile.getName());
    console.log('Image URL: ' + profile.getImageUrl());
    console.log('Email: ' + profile.getEmail());
  }
  </script>
</head>
<body>
  <div class="g-signin2" data-onsuccess="onSignIn"></div>
</body>
</html>

When I click Sign-in button, it popups a window, and after logged in, it gave me 400 error as below: 当我单击“登录”按钮时,它会弹出一个窗口,并在登录后给我400错误,如下所示:

400. That’s an error.

The requested URL was not found on this server. That’s all we know.

I guess it's redirect issue, but I don't know how to configure it. 我想这是重定向问题,但我不知道如何配置它。 So I checked url it returned to me: 所以我检查了网址,它返回了我:

https://accounts.google.com/o/oauth2/auth?fetch_basic_profile=true&scope=email+profile+openid&response_type=permission&e=3100087&redirect_uri=storagerelay://http/127.0.0.1:8000?id%3Dauth867179&ss_domain=http://127.0.0.1:8000&client_id=378468243311-9dt7m9ufa9mee305j1b12815put5betb.apps.googleusercontent.com&openid.realm&hl=en&from_login=1&as=1b5f0a407ea58e11&pli=1&authuser=0

Why is redirect_uri "storagerelay://http/127.0.0.1:8000?id%3Dauth867179"? 为什么redirect_uri是“ storagerelay://http/127.0.0.1:8000?id%3Dauth867179”?

Your code should work now. 您的代码现在应该可以工作了。

Google fixed this issue. Google已解决此问题。 Origins with a trailing slash are also accepted now (Origin can be set in Google Developers Console > APIs & auth > Credentials). 现在也接受带有斜杠的起源(可以在Google Developers Console> API和身份验证>凭据中设置来源)。

Hi I faced exactly same problem. 嗨,我面临着完全相同的问题。

Regarding my case, the problem is that OAuth try to redirect to url of "127.0.0.1?id=authxxxxx" where xxxxx is random 5 digits. 对于我的情况,问题是OAuth尝试重定向到“ 127.0.0.1?id=authxxxxx”的网址,其中xxxxx是随机的5位数字。

Thus I added following four urls at Redirect URI Google Developers Console. 因此,我在重定向URI Google Developers Console中添加了以下四个URL。 I am not sure which one hits the rule, but I could workaround with this 我不确定哪个人符合规则,但是我可以解决此问题

(1) http://127.0.0.1/ * (1) http://127.0.0.1/ *
(2) https of (1) (2)个(1)的https
(3) http://127.0.0.1 (3) http://127.0.0.1
(4) https of (3) (4)(3)的https

Hope it also works for you. 希望它也对您有用。

To test on localhost environment, here's things we should to do: 要在本地主机环境上进行测试,我们应该做以下事情:

  1. Add " http://localhost:8000 " to "JavaScript origins" 将“ http:// localhost:8000 ”添加到“ JavaScript来源”
  2. Remove everything from "Redirect URIs", because JavaScript API doesn't need any redirect URIs 从“重定向URI”中删除所有内容,因为JavaScript API不需要任何重定向URI

[1] https://developers.google.com/identity/sign-in/web/devconsole-project [1] https://developers.google.com/identity/sign-in/web/devconsole-project

Redirect URIs 重定向URI

http://localhost:8000/      this **/** at end solve my problem

JavaScript origins JavaScript的起源

http://localhost:8000

NOTE: localhost and 127.0.0.1 conflicts can create problems for you sometime 注意:本地主机和127.0.0.1冲突有时会为您造成问题

I'm also having the same problem, and was able to get past it by using "localhost" instead of the ip address. 我也遇到了同样的问题,并且可以通过使用“ localhost”而不是IP地址来克服它。 It's not a great solution, but it was sufficient for me to continue debugging in dev, so I just thought I'd mention it. 这不是一个很好的解决方案,但是对于我来说,继续在开发人员中进行调试就足够了,所以我以为我会提到它。

edit: keep in mind that you will have to add http://localhost to your Google Oauth Javascript Origins. 编辑:请记住,您必须将http:// localhost添加到Google Oauth Javascript Origins。

I had the same problem yesterday. 我昨天有同样的问题。 I had OAuth credentials with localhost and my-server.com . 我拥有localhostmy-server.com OAuth凭据。 So, I fixed this problem creating a new OAuth credentials only for my-server.com , without localhost . 因此,我解决了此问题,仅为my-server.com创建了新的OAuth凭据,而没有localhost Obviusly, my app uses these new credentials. 显然,我的应用程序使用这些新凭据。

我发现清除ACCOUNT_CHOOSER accounts.google.com cookie后该错误消失了。

Replace everywhere http://127.0.0.1:8000/ to HTTP://localhost:8000/ even in your browser too. 即使在浏览器中,也将http://127.0.0.1:8000/替换为HTTP://localhost:8000/ The Problem comes when you use http://127.0.0.1:8000/ either in the developer console or a browser. 在开发者控制台或浏览器中使用http://127.0.0.1:8000/时,就会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM