0

我正在尝试使用 facebook javascript SDK 让我的应用程序连接到用户的 facebook 帐户。

在运行代码连接它之前,有没有一种简单的方法来检查我的应用程序是否已经连接到 Facebook?它必须重新加载页面,这样用户才能真正看到他们是通过 facebook 登录的,但是如果我这样做 location.reload(); 最后它会进入一个无限循环,因为这段代码在标题中,它将再次运行。标题位于每个页面上,因此重定向到其他地方无济于事。

使 facebook 登录按钮起作用的 js 代码,主要来自 facebook 的分步指南:

<script>
function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    if (response.status === 'connected') {
        testAPI();
    } else {
        document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
    }
}

window.fbAsyncInit = function () {
    FB.init({
        appId: '(my app id)',
        cookie: true,
        xfbml: true,
        version: 'v2.8'
    });

    FB.getLoginStatus(function (response) {
        statusChangeCallback(response);
    });

};
(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/debug.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function (response) {
        console.log('Successful login for: ' + response.name);
        document.getElementById('status').innerHTML =
            'Thanks for logging in, ' + response.name + '!';
    });
    get_fb_info();
}

function get_fb_info() {
    FB.api('/me', 'GET', {fields: 'first_name,last_name,name,email,id,picture'}, function (response) { //add email,
        console.log("FB NAME: " + response.name);
        console.log("FB EMAIL: " + response.email);

        data_string = 'fb_id=' + response.id + '&f_name=' + response.first_name + '&l_name=' + response.last_name + '&name=' + response.name + '&email=' + response.email + '&picture=' + response.picture;
        $.ajax({
            type: "POST",
            url: "ajax_fb_login.php",
            data: data_string,
            success: function (data) {
                console.log(data);
                //location.reload(); only has to happen once
            }
        })
    });
}
</script>

谢谢 :)

4

1 回答 1

3

根据官方文档,类似以下内容应该可以解决问题:

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;
  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app
  } else {
    // the user isn't logged in to Facebook.
  }
 });

FB.getLoginStatus() 允许您确定用户是否登录到 Facebook 并验证了您的应用程序。用户有三种可能的状态:

  1. 用户已登录 Facebook 并已验证您的应用程序(已连接)
  2. 用户已登录 Facebook,但尚未验证您的应用程序 (not_authorized)
  3. 用户要么未登录 Facebook,要么明确退出您的应用程序,因此它不会尝试连接到 Facebook,因此,我们不知道他们是否已经验证了您的应用程序(未知)
于 2017-08-08T18:45:37.207 回答