WebApplication Google登录

时间:2017-03-13 15:21:30

标签: web-applications login google-api google-signin gapi

我试图这样做:https://developers.google.com/identity/sign-in/web/sign-in 所以在我的index.html中我添加了这个:

<script src="https://apis.google.com/js/platform.js" async defer></script>
<meta name="google-signin-client_id" content="changedkey.apps.googleusercontent.com ">

在login.html上我尝试使用它:

<div class="g-signin2" data-onsuccess="onSignIn"></div>

还有这个:

<div id="my-signin2">Google </div>
                      <script>
                        function onSuccess(googleUser) {
                          console.log('Logged in as: ' + googleUser.getBasicProfile().getName());
                        }
                        function onFailure(error) {
                          console.log(error);
                        }
                        function renderButton() {
                          gapi.signin2.render('my-signin2', {
                            'scope': 'profile email',
                            'width': 240,
                            'height': 50,
                            'longtitle': true,
                            'theme': 'dark',
                            'onsuccess': onSuccess,
                            'onfailure': onFailure
                          });
                        }
                      </script>

但它们都不起作用,我按下按钮,没有任何反应。我做错了什么?

编辑: 我将代码更改为:

<script>
                  var googleUser = {};
                  var startApp = function() {
                    gapi.load('auth2', function(){
                      // Retrieve the singleton for the GoogleAuth library and set up the client.
                      auth2 = gapi.auth2.init({
                        client_id: 'MYKEY.apps.googleusercontent.com',
                        cookiepolicy: 'single_host_origin',
                        // Request scopes in addition to 'profile' and 'email'
                        //scope: 'additional_scope'
                      });
                      attachSignin(document.getElementById('google'));
                    });
                  };

                  function attachSignin(element) {
                    console.log(element.id);
                    auth2.attachClickHandler(element, {},
                        function(googleUser) {
                          document.getElementById('name').innerText = "Signed in: " +
                              googleUser.getBasicProfile().getName();
                        }, function(error) {
                          alert(JSON.stringify(error, undefined, 2));
                        });
                  }
                  </script>

按钮就像这样:

  <div id="google" type="button"><img src="assets/login/google-logo.png"><span class="google-title">GOOGLE</span></div>

它进入该功能,为Google登录创建窗口,但总是返回一个弹出窗口,上面写着:&#34;错误&#34;:&#34; popup_closed_by_user&#34; 为什么会这样? 我也在控制台中得到这个: https://s3.amazonaws.com/uploads.hipchat.com/39260/829560/nGKpPpPQ1vlvC27/upload.png

1 个答案:

答案 0 :(得分:0)

我在这里找到答案: Google API authentication: Not valid origin for the client 我的问题是我使用的是服务器创建的cliendID而不是新创建的cliendID,当我这样做时,我已经设置了原点,现在它可以正常工作