google sign in not returning error also not signing in

浪尽此生 提交于 2019-12-11 03:24:54

问题


I am trying to make google sign in work on my intranet site which currently uses integrated windows authentication - I want to get away from that because I need to support ChromeBooks and pretty much everything here is going google......

This is what IS working:

  • I get a sign in button. I can see in the console log that it is using my google developer client id:

XHR finished loading: GET "https://accounts.google.com/o/oauth2/iframerpc?action=checkOrigin&origin=ht…d=777682448638-5tpyaddayaddyadddarvnbr3p6.apps.googleusercontent.com".

  • If I click the button on a machine where I am not logged into google I will geta prompt to log into google.

  • If I click the button on a machine where I am logged into google but have not visited this site before then I will get the authorization request for my email and profile to be shared with my web application.

    • If I have done the above, and click the button then there is a flash of a pop-up window that goes away immediately.

What is NOT working:

  • My website never gets any onSuccess back....... so the button ALWAYS says "Sign in with Google" I don't get to the next step of "Signed in as molly ......"

Here is a shortended version of my code - please note that right now my website uses the integrated windows authentication - that is what I am trying to port away from so we can use ChromeBooks and other computers that are not using our windows login.

this is from my _Layout.cshtml page

    @{
        var username = WebSecurity.CurrentUserName;
    }

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>Rutland City Public Schools - ACADEMIC SITE</title>
        <link href="~/Styles/Styles.css" rel="stylesheet" type="text/css"/>
        <link href="~/Styles/PrintStyles.css" rel="stylesheet" type="text/css" media="print"/>
        <meta http-Equiv="Cache-Control" Content="no-cache">
        <meta http-Equiv="Pragma" Content="no-cache">
        <meta http-Equiv="Expires" Content="0">
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
        <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
        @RenderSection("script", required: false)
        <meta name="google-signin-scope" content="profile email">
        <meta name="google-signin-client_id" content="777682448638-5tpyaddayaddayaddavnbr3p6.apps.googleusercontent.com">

    </head>

    <body>

          <div id="topRight">  
              Hello: @username   <br> <br> 

                <div id="my-signin2" align="center" ></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': 'https://www.googleapis.com/auth/plus.login',
                        'width': 150,
                        'height': 30,
                        'longtitle': true,
                        'theme': 'light',
                        'onsuccess': onSuccess,
                        'onfailure': 'oops!'
                      });

                    }
                  </script>

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

              ignore google button!
          </div>

          <div id="mainContent">
               @RenderBody()
          </div> 

    </body> 

    </html>

If I open up "Inspect Element" when I am in chrome I can see that under the network tab it clearly goes off and does some gets to google. In the console tab I only see the XHR finished loading: GET "https://accounts.google................." message.

What is going on here? Why am I not getting the onSuccess?

Thank you!!!!

Added more notes:

I added a link:

 <a href="#" onclick="signIn();">Sign in</a>

that calls this function:

 function signIn() {
    var auth2 = gapi.auth2.getAuthInstance();
    console.log(auth2);

From the console results I can see my website, and my client id, but again I see nothing about my actual user.

jF {zd: Object, po: "single_host_origin", zt: true, ha: true, G: undefined…}
B: bY
B: "777682448638-5tp3rss17g1qarc3em0opcr4rvnbr3p6.apps.googleusercontent.com"
Db: "http://rcps"
Ei: undefined
El: undefined
G: "google"
Ka: false
Ld: Object
openid.realm: undefined
redirect_uri: "http://rcps/academic/academic"
response_type: "token id_token"
scope: "openid profile email"

回答1:


With the Google Sign-In (gapi.auth2) JavaScript client libraries, you should probably be using listeners to check the state of the client. I'm not sure exactly how you're rendering the button but you might want to take a good look at the Google Sign-in quickstart.

Although I have found using listeners to have advantages over using the success callback, the following code works for me (I see onWin upon sign-in with the z variable containing the authorization response):

function onFail(z){ alert('Fail' + JSON.stringify(z)); }                                                                                  
function onWin(z){ alert('Win' + JSON.stringify(z)); }
gapi.load('auth2', function() {
  gapi.signin2.render('signin-button', {
    scope: 'https://www.googleapis.com/auth/plus.login',
    onsuccess: onWin,                                                     
    onfail: onFail,   
    fetch_basic_profile: false });
  gapi.auth2.init({fetch_basic_profile: false,
      scope:'https://www.googleapis.com/auth/plus.login'}).then(
          function (){
            console.log('init');
            auth2 = gapi.auth2.getAuthInstance();
            auth2.isSignedIn.listen(updateSignIn);
            auth2.then(updateSignIn());
          });
});

Do things when sign-in changes (e.g. show/hide authorized UI):

var updateSignIn = function() {
  console.log('update sign in state' + auth2.isSignedIn.get());
}

At any point when auth2.isSignedIn.get() returns true, you can use auth2.currentUser.get() to retrieve the profile data for the currently signed in user.




回答2:


I had similar problems when testing on localhost. When I pushed my code onto my production server the simple example provided by Google worked perfectly as advertised. The button's values changes or stays accordingly even after a page refresh.




回答3:


Perhaps your browser is configured to block third party cookies. If so, disable that feature. You may also want to have a note on your login page mentioning that third party cookies are required so that your users will know to do the same.



来源:https://stackoverflow.com/questions/30895708/google-sign-in-not-returning-error-also-not-signing-in

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