如何仅注销我的FBapplication而不是我的facebook帐户?

时间:2014-02-26 05:16:23

标签: javascript php facebook facebook-graph-api

我在我的网络应用程序上使用facebook javascript sdk。我正在使用graph api登录我的应用程序。当我从我的应用程序注销时,我的应用程序被注销,我的Facebook帐户也会注销。

如何仅注销我的应用程序而不是我的Facebook帐户?

如果有人为此找到解决方案,请帮助我。

代码:

<script type="text/javascript">     

    var button;
    var userInfo;

    window.fbAsyncInit = function() {
        FB.init({ appId: '########', 
            status: true, 
            cookie: true,
            xfbml: true,
            oauth: true});

       showLoader(true);

       function updateButton(response) {
            button       =   document.getElementById('fb-auth');
            userInfo     =   document.getElementById('user-info');
            userdata =   document.getElementById('user-data');
            if (response.authResponse) {
                //user is already logged in and connected
                FB.api('/me', function(info) {
                    login(response, info);
                });

                button.onclick = function() {
                    FB.logout(function(response) {
                        logout(response);
                    });
                };
            } else {
                //user is not connected to your app or logged out
                button.innerHTML = 'Login';
                button.onclick = function() {
                    showLoader(true);
                    FB.login(function(response) {
                        if (response.authResponse) {
                            FB.api('/me', function(info) {
                                login(response, info);
                            });    
                        } else {
                            //user cancelled login or did not grant authorization
                            showLoader(false);
                        }
                    },                                    {scope:'email,user_birthday,status_update,publish_stream,user_about_me'});   
                }
            }
        }

        // run once with current status and whenever the status changes
        FB.getLoginStatus(updateButton);
        FB.Event.subscribe('auth.statusChange', updateButton);  
    };
    (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol 
            + '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
    }());


    function login(response, info){
        if (response.authResponse) {
            var accessToken                                 =   response.authResponse.accessToken;

            userInfo.innerHTML                             = '<img src="https://graph.facebook.com/' + info.id + '/picture">' + info.name
                                                             + "<br /> Your Access Token: " + accessToken;


    button.innerHTML                               = 'Logout';
            showLoader(false);
            document.getElementById('other').style.display = "block";

        }
    }

    function logout(response){
        userInfo.innerHTML                             =   "";
        document.getElementById('debug').innerHTML     =   "";
        document.getElementById('other').style.display =   "none";
        showLoader(false);
    }

     //stream publish method
     function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){
        showLoader(true);
        FB.ui(
        {
            method: 'stream.publish',
            message: '',
            attachment: {
                name: name,
                caption: '',
                description: (description),
                href: hrefLink
            },
            action_links: [
                { text: hrefTitle, href: hrefLink }
            ],
            user_prompt_message: userPrompt
        },
        function(response) {
            showLoader(false);
        });

    }
    function showStream(){
        FB.api('/me', function(response) {
            //console.log(response.id);
            streamPublish();
        });
    }           

  function share(){
        showLoader(true);
        var share = {
            method: 'stream.share',
            u: 'http://www.appovative.com/'
        };

        FB.ui(share, function(response) { 
            showLoader(false);
            console.log(response); 
        });
    }



    function setStatus(){
        showLoader(true);

        status1 = document.getElementById('status').value;
        FB.api(
          {
            method: 'status.set',
            status: status1
          },
          function(response) {
            if (response == 0){
                alert('Your facebook status not updated. Give Status Update        Permission.');
            }
            else{
                alert('Your facebook status updated');
            }
            showLoader(false);
          }
        );
    }

    function showLoader(status){
        if (status)
            document.getElementById('loader').style.display = 'block';
        else
            document.getElementById('loader').style.display = 'none';
    }

</script>

1 个答案:

答案 0 :(得分:0)

它们是在FB对象中定义的函数,用它来销毁FB会话,

用法(通过PHP)

$facebook->destroySession();

FB API提供logoutURL,用于将用户从当前的Facebook帐户中记录下来。

你可以像这样使用它,

$facebook = new Facebook($config);
$params = array('next' => 'www.yousite.com/test.php' );
$logoutURL = $facebook->getLogoutUrl($params);

***注 建议也添加以下代码(如果您不希望您的用户退出您的网站,则不会这样)

//remove PHPSESSID from browser
if ( isset( $_COOKIE[session_name()] ) )
    setcookie( session_name(), '', time()-7000000, '/' );
//clear session from globals
    $_SESSION = array();

使用JS使用自定义事件 参考:(FB.logout() called without an access token. javascript sdk

function fbLogoutUser() {
    FB.getLoginStatus(function(response) {
        if (response && response.status === 'connected') {
            FB.logout(function(response) {
                document.location.reload();
            });
        }
    });
}