Facebook JavaScript SDK - 保存访问令牌

时间:2017-04-14 10:10:14

标签: javascript facebook facebook-javascript-sdk

我尝试使用Facebook JavaScript SDK。我有我的代码,但它不能很好地工作,我发现this。我的网站上的问题是,如果我登录并重新加载页面,那么我丢失了数据,我的意思是访问令牌没有保存,就像没有连接一样。正如您在我添加的链接上看到的那样,您可以登录,刷新页面,几秒钟后(不是立即)加载所有内容。我应该在代码中添加什么?我把它粘贴在这里但是现在它与网站相同 - 我只需要理解为什么我的令牌没有保存(为什么我在重新加载后丢失它)。在控制台中,登录并刷新后,尝试注销我已经FB.logout() called without an access token.



window.fbAsyncInit = function() {
FB.init({
      appId      : 'xxxxxxxxxxxxxxx',
      cookie     : true,  // enable cookies to allow the server to access the session
      xfbml      : true,  
      version    : 'v2.8' 
    });
    
    // Check whether the user already logged in
	FB.getLoginStatus(function(response) {
		if (response.status === 'connected') {
			getFbUserData();
		}
	});
};


(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));


function fbLogin() {
    FB.login(function (response) {
        if (response.authResponse) {
            getFbUserData();
        } else {
            document.getElementById('status').innerHTML = 'User cancelled login or did not fully authorize.';
        }
    }, {scope: 'email'});
}

function getFbUserData(){
    FB.api('/me', {locale: 'en_US', fields: 'email'},
    function (response) {
        document.getElementById('fbLink').setAttribute("onclick","fbLogout()");
document.getElementById('fbLink').innerHTML = 'Logout from Facebook';
document.getElementById('status').innerHTML = 'Thanks for logging in, ' + response.email + '!';
    
    });
}


function fbLogout() {
    FB.logout(function() {
        document.getElementById('fbLink').setAttribute("onclick","fbLogin()");
document.getElementById('userData').innerHTML = '';
document.getElementById('status').innerHTML = 'You have successfully logout from Facebook.';
    });
}
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="status"></div>

<!-- Facebook login or logout button -->
<a href="javascript:void(0);" onclick="fbLogin()" id="fbLink"></a>

<!-- Display user profile data -->
<div id="userData"></div>
&#13;
&#13;
&#13;

0 个答案:

没有答案