Google OAuth登录问题

时间:2018-04-07 08:37:41

标签: javascript php oauth oauth-2.0 google-oauth

首先,当我点击按钮,然后出现谷歌登录对话框,我登录,它显示我的个人资料信息,但当我刷新页面,然后它显示我没有登录,即使我已经登录。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

<title>Google Login JavaScript Example</title>


<meta charset="UTF-8">


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

<script>


/**
 * The Sign-In client object.
 */
var auth2;

/**
 * Initializes the Sign-In client.
 */
var initClient = function() {
    gapi.load('auth2', function(){
        /**
         * Retrieve the singleton for the GoogleAuth library and set up the
         * client.
         */
        auth2 = gapi.auth2.init({
            client_id: '961018387640-uh2bbqior4gcmi2o8hdnuahjb3kombfo.apps.googleusercontent.com'
        });

         // auth2 = gapi.auth2.init();


       // alert(auth2 );

      //  alert(auth2.isSignedIn.get());





    if (gapi.auth2.getAuthInstance().isSignedIn.get()) 
    {

        var profile = auth2.currentUser.get().getBasicProfile();


        document.getElementById("userdetail").innerHTML += '<hr> ID: ' + profile.getId();


        document.getElementById("userdetail").innerHTML += '<hr>Full Name: ' + profile.getName();


        document.getElementById("userdetail").innerHTML += '<hr>Given Name: ' + profile.getGivenName();


        document.getElementById("userdetail").innerHTML += '<hr>Family Name: ' + profile.getFamilyName();


        document.getElementById("userdetail").innerHTML += '<hr>Image URL: ' + profile.getImageUrl() + '<img src="'+profile.getImageUrl()+'">';


        document.getElementById("userdetail").innerHTML += '<hr>Email: ' + profile.getEmail();



        console.log('ID: ' + profile.getId());


        console.log('Full Name: ' + profile.getName());


        console.log('Given Name: ' + profile.getGivenName());


        console.log('Family Name: ' + profile.getFamilyName());


        console.log('Image URL: ' + profile.getImageUrl());

        console.log('Email: ' + profile.getEmail());


    }
    else
    {

        document.getElementById("userdetail").innerHTML += '<hr><h1> NOT LOGGED IN: ' +   '</h1>';




    }







        // Attach the click handler to the sign-in button
        auth2.attachClickHandler('signin-button', {}, onSuccess, onFailure);






    });
};


//initClient();


/**
 * Handle successful sign-ins.
 */
var onSuccess = function(user) {
    console.log('Signed in as ' + user.getBasicProfile().getName());
document.getElementById("userdetail").innerHTML = '<hr>  <hr>  <h2> Signed in as ' + user.getBasicProfile().getName() + '</h2> '; // Do not send to your backend! Use an ID token instead.



    if (gapi.auth2.getAuthInstance().isSignedIn.get()) 
    {

        var profile = auth2.currentUser.get().getBasicProfile();


        document.getElementById("userdetail").innerHTML += '<hr> ID: ' + profile.getId();


        document.getElementById("userdetail").innerHTML += '<hr>Full Name: ' + profile.getName();


        document.getElementById("userdetail").innerHTML += '<hr>Given Name: ' + profile.getGivenName();


        document.getElementById("userdetail").innerHTML += '<hr>Family Name: ' + profile.getFamilyName();


        document.getElementById("userdetail").innerHTML += '<hr>Image URL: ' + profile.getImageUrl() + '<img src="'+profile.getImageUrl()+'">';


        document.getElementById("userdetail").innerHTML += '<hr>Email: ' + profile.getEmail();



        console.log('ID: ' + profile.getId());


        console.log('Full Name: ' + profile.getName());


        console.log('Given Name: ' + profile.getGivenName());


        console.log('Family Name: ' + profile.getFamilyName());


        console.log('Image URL: ' + profile.getImageUrl());

        console.log('Email: ' + profile.getEmail());


    }





     };

/**
 * Handle sign-in failures.
 */
var onFailure = function(error) {
    alert(error);
    console.log(error);
};



    //gapi.auth2.getAuthInstance().isSignedIn.get()
/*
 if (auth2.isSignedIn.get() == true) {
    auth2.signIn();
  }
  */

    function onSignIn(googleUser) 
    {
            var profile = googleUser.getBasicProfile();
            console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
            console.log('Name: ' + profile.getName());
            console.log('Image URL: ' + profile.getImageUrl());
            console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.

            document.getElementById("userdetail").innerHTML = '<hr>ID: ' + profile.getId(); // Do not send to your backend! Use an ID token instead.
            document.getElementById("userdetail").innerHTML += '<hr>Name: ' + profile.getName();
            document.getElementById("userdetail").innerHTML +='<hr>Image URL: ' + profile.getImageUrl();
            document.getElementById("userdetail").innerHTML +='<hr>Email: ' + profile.getEmail(); // This is null if the 'email' scope is not present.
    }

</script> 


</head>



 <body> 

 <button class="signin-button"  id="signin-button">signin-button</button>



     <div id="userdetail"></div>

</body>
</html>

0 个答案:

没有答案