TypeError: gapi.auth2 undefined

孤街醉人 提交于 2019-11-29 01:46:02

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/29815870/typeerror-gapi-auth2-undefined

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!