Search code examples
web-applicationsauthenticationgoogle-apigoogle-signingoogle-api-js-client

WebApplication Google Sign in


I'm trying to do this: https://developers.google.com/identity/sign-in/web/sign-in So in my index.html I added this:

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

On login.html I tried using this:

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

And also this:

<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>

But they both do not work, and I press the button and nothing happens. What have I done wrong?

EDIT: I changed the code to look like this:

<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>

And the button like this:

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

And it enters the function, creates the window for Google Log in, but ALWAYS returns back an popup that says: "error": "popup_closed_by_user" Why is this happening? Also I get this in the console: https://s3.amazonaws.com/uploads.hipchat.com/39260/829560/nGKpPpPQ1vlvC27/upload.png


Solution

  • I found the answer here: Google API authentication: Not valid origin for the client My problem was that I was using a Server created cliendID instead of a newly created one, and when I did that, I've set the origin and now it works