gapi.auth.signOut();不工作我迷路了

时间:2014-02-28 04:36:23

标签: oauth oauth-2.0 google-plus google-oauth

以下是我用来登录谷歌的代码。我在login.php上有一个带有id authorize-button的元素。点击它就可以正常登录。

我的头文件中有一个注销链接。当我点击退出时,它会调用gapi.auth.signOut();然后它会销毁会话并重定向回login.php

就我所知,这种情况发生了但是它只是用谷歌将用户重新登录到我们的网站。这是一个痛苦,因为我们的一些用户从谷歌切换到Facebook登录。

提前感谢您的帮助。

function handleClientLoad() {
    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth, 1);
}

function checkAuth() {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}

function handleAuthResult(authResult) {
    var authorizeButton = document.getElementById('authorize-button');


    if (authResult && !authResult.error) {
        //authorizeButton.style.visibility = 'hidden';
        makeApiCall();
    } else {
        //authorizeButton.style.visibility = '';
        authorizeButton.onclick = handleAuthClick;
    }
}

function handleAuthClick(event) {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
    return false;
}

function signOut() {
    gapi.auth.signOut();
}


function makeApiCall() {

    gapi.client.load('oauth2', 'v2', function() {
        var request = gapi.client.oauth2.userinfo.get();

        request.execute(function(logResponse) {

            var myJSON = {
                "myFirstName": logResponse.given_name,
                "myLastName": logResponse.family_name,
                "name": logResponse.name,
                "socialEmailAddress": logResponse.email
            };

            gapi.client.load('plus', 'v1', function() {

                var request = gapi.client.plus.people.get({
                    'userId': 'me'
                });
                request.execute(function(logResponse2) {
                    //alert(JSON.stringify(logResponse));
                    myJSON['profilePicture'] = logResponse2.image.url;
                    myJSON['socialId'] = logResponse2.id;
                    //alert(JSON.stringify(myJSON));
                    $.ajax({
                        type: "POST",
                        url: "includes/login-ajax.php",
                        data: "function=googleLogin&data=" + JSON.stringify(myJSON),
                        dataType: "html",
                        success: function(msg) {

                            if (msg == 1) {

                                //window.location = "settings.php";
                            }
                        }
                    });
                });
            });
        });
    });
}

3 个答案:

答案 0 :(得分:28)

确保您已在登录按钮代码中将Cookie政策设置为none以外的值。例如:

function handleAuthClick(event) {
  gapi.auth.authorize(
    {
      client_id: clientId, 
      scope: scopes, 
      immediate: false, 
      cookie_policy: 'single_host_origin'
    },
    handleAuthResult);
  return false;
}

请注意,如果从localhost运行,则注销将无效。

答案 1 :(得分:1)

奇怪的问题,但即使用户经过身份验证,也可以通过呈现登录按钮(隐藏)来解决我的问题。

请在此处查看完整的问题/答案https://stackoverflow.com/a/19356354/353985

答案 2 :(得分:1)

我今天遇到了同样的问题。我一直在寻找解决方案。对我有用的唯一可靠解决方案是通过解释here

撤销

我在撤销期间需要会话中存储access_token

以下是我可能认为有用的代码

      function logout() {
         var access_token = $('#<%=accessTok.ClientID %>').val();
         var provider = $('#<%=provider.ClientID %>').val();
    if (access_token && provider) {
        if (provider == 'GPLUS') {
            var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token=' +
                access_token;

            // Perform an asynchronous GET request.
            $.ajax({
                type: 'GET',
                url: revokeUrl,
                async: false,
                contentType: "application/json",
                dataType: 'jsonp',
                success: function (nullResponse) {
                    // Do something now that user is disconnected
                    // The response is always undefined.
                },
                error: function (e) {
                    // Handle the error
                    // console.log(e);
                    // You could point users to manually disconnect if unsuccessful
                    // https://plus.google.com/apps
                }
            });
        }
        else if (provider == 'FB') {
            FB.getLoginStatus(function (response) {
                if (response.status === 'connected') {
                    FB.logout();
                }
            });
        }
    } else {

    }
}