TypeError: gapi.auth2 undefined
Asked Answered
U

1

38

I went exactly by the instructions for integrating google sign-in:

https://developers.google.com/identity/sign-in/web/sign-in#specify_your_apps_client_id

sign-in works, but sign-out gives a javascript error in the line:

var auth2 = gapi.auth2.getAuthInstance();

The error is:

gapi.auth2 undefined

I include the google platform library as instructed:

    <script type='text/javascript' src='https://apis.google.com/js/platform.js' async defer></script>

Why does it not work?

Upstart answered 23/4, 2015 at 6:55 Comment(1)
I have a quite minimalistic example of using Google drive (which also requires authentication) in javascript. The entire sample is fully contained in a single html page of 170 lines: dannyruijters.nl/webtex/googledrive.html Maybe that helps you to resolve your problem.Sarene
R
79

Are signIn and signOut used on the same page? Div g-signin2 loads and inits gapi.auth2 so it should work as long as those are on the same page.

In case signOut is on separate page, you should manually load and init gapi.auth2 library.

Full example (you have to replace YOUR_CLIENT_ID with your actual client_id):

<html>
<head>
   <meta name="google-signin-client_id" content="YOUR_CLIENT_ID">
</head>
<body>
  <script>
    function signOut() {
      var auth2 = gapi.auth2.getAuthInstance();
      auth2.signOut().then(function () {
        console.log('User signed out.');
      });
    }

    function onLoad() {
      gapi.load('auth2', function() {
        gapi.auth2.init();
      });
    }
  </script>
  <a href="#" onclick="signOut();">Sign out</a>

  <script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>
</body>
</html>
Ruthenious answered 23/4, 2015 at 19:53 Comment(8)
Is gapi.load documented anywhere? It doesn't appear to be part of the reference material: developers.google.com/identity/sign-in/web/referenceMahayana
Same sentiments here @MicahZoltu. My wild guess if you have the <div class="g-signin2"></div> button on your page (which all the documentation examples) the API will be loaded for you since the button uses that, but for instances where we don't have the button you must tell the gapi object which API(s) to load since it encapsulates all of Google's platform APIs (and probably finds it unwise to load all of them)Haaf
thanks for the solution,, i was stuck in this for a long timeWoven
Cool!!!!!!!!!!!!!!!!!!!!! That's what i was loooking for.....my signout is on another page.. can we see following? GoogleUser = GoogleAuth.currentUser.get(); if (GoogleUser.isSignedIn()) { document.getElementById("demo").innerHTML = "user is signed in already"; } else { document.getElementById("demo").innerHTML = "no signed in "; }Slider
@Jarosław Gomułka I have used your code ...but when i try to log in again it doesn't ask credentials, it directly log ins in last google account...Why it doent get completely logged out??Slider
@Slider In our project when we added an auth2.disconnect() before the auth2.signOut() we got complete logout, the desired result.Mord
Can I put the javascript in an external file?Griffiths
FIY, assume that for some reason, you reload the page after a user signs in using Google Sign In, then, you need to manually load and init gapi.auth2 library.Saunders

© 2022 - 2024 — McMap. All rights reserved.