Google SignIn登录脚本无效

Sta*_*y J 4 google-authentication gapi google-oauth2 google-signin

我使用以下脚本来集成谷歌身份验证

//google auth code
gapi.load('auth2', function() {//load in the auth2 api's, without it gapi.auth2 will be undefined
    gapi.auth2.init(
        {
            client_id: '1063305095705-k68gpfhi46tbu6kv8e8tidc751hte5pb.apps.googleusercontent.com'
        }
    );
    var GoogleAuth   = gapi.auth2.getAuthInstance();//get's a GoogleAuth instance with your client-id, needs to be called after gapi.auth2.init
    $scope.onLogInButtonClick=function(){//add a function to the controller so ng-click can bind to it
        GoogleAuth.signIn().then(function(response){//request to sign in
            console.log(response);
            console.log(response.wc.wc);
            console.log(response.wc.hg);
            console.log(response.wc.Ph);
        });
    };
});
Run Code Online (Sandbox Code Playgroud)

我试图在其他一些应用程序中使用它并收到以下错误: -

gapi.auth2.ExternallyVisibleError: gapi.auth2 has been initialized with
different options. Consider calling gapi.auth2.getAuthInstance() 
instead of gapi.auth2.init().
Run Code Online (Sandbox Code Playgroud)

我也包括在内

<script src="https://apis.google.com/js/platform.js?key=xxxxxxxxx"></script>
Run Code Online (Sandbox Code Playgroud)

一切似乎都很好,不明白为什么它不起作用..

ped*_*uan 8

下一个代码应该有所帮助:我已经花了两个小时来解决这个问题,因为我收到了同样的错误,我决定使用onLoad函数和自定义谷歌登录按钮.

有些观点,如果我们不想从用户那里获取个人资料信息,那可能是必要的......

  • 使用延迟异步方法时,如下面的代码中使用onload函数:

https://apis.google.com/js/platform.js?onload = onLoadCallback

  • 使用signIn功能上的click事件创建自己的登录按钮
<a onclick="signIn();">Google Sign In</a>
Run Code Online (Sandbox Code Playgroud)

完整的解决方案是:

<script src="https://apis.google.com/js/platform.js?onload=onLoadCallback" async defer></script>

<meta name="google-signin-client_id" content="YOUR_ID.apps.googleusercontent.com"/>

<!-- I have commented default google sign in button because it didn't allow me to make my own gapi.load -->
<!--<div class="g-signin2" data-onsuccess="ConSignIn"></div>-->

<a onclick="signIn();">Google Sign In</a>

<script>

    function onLoadCallback() {

      console.log('onLoadCallback');
      gapi.load('auth2', function() {
        gapi.auth2.init({
            client_id: 'YOUR_ID.apps.googleusercontent.com',
            //This two lines are important not to get profile info from your users
            fetch_basic_profile: false,
            scope: 'email'
        });        
      });     
    }

    function signIn() {
        console.log('I am passing signIn')

        var auth2 = gapi.auth2.getAuthInstance();         
          // Sign the user in, and then retrieve their ID.
          auth2.signIn().then(function() {
            console.log(auth2.currentUser.get().getId());               
          });         
     }

</script>
Run Code Online (Sandbox Code Playgroud)