1

根据 Facebook Javascript SDK文档

调用 FB.init 时,可以通过设置 status: true 来检查用户的状态。

要接收此调用的响应,您必须订阅 auth.statusChange 事件。

我相信我这样做是正确的,但是我的示例中的“状态更改”日志永远不会触发:

window.fbAsyncInit = function() {
    FB.init({
        appId      : 'xxxxxxxxxxxx', // App ID
        channelUrl : '../channel.html', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
    });

    FB.Event.subscribe('auth.statusChange', function(response) {
        console.log("status change!");
    });
    ...
});

该文档不正确吗?还是我做错了什么?

4

2 回答 2

0

我认为最后而不是 }); 是 };

也可能是你没有开始。这对我有用:

$(document).ready(function() {

  var app_id = 'xxxxxxxxxxxxxx';

  var start = function (){


    window.fbAsyncInit = function() {

     FB.init({

        appId     : app_id,
        status    : true,
        cookie    : true,
        xfbml     : true,
        version   : 'v2.7'

     });

     FB.Event.subscribe('auth.statusChange', function(response) {
        var status = response.status;
        console.log(response.status);

        if (status === "connected") {
         //
        }

     });
   };

  }

  start();

 });
于 2016-08-09T16:55:40.587 回答
0

你可以试试这个方法。

<script type="application/javascript">

    function statusChangeCallback(response) {  // Called with the results from FB.getLoginStatus().
        console.log('statusChangeCallback');
        console.log(response);                   // The current login status of the person.
        if (response.status === 'connected') {   // Logged into your webpage and Facebook.
            console.log("User connected, name: " + response.name);
        } else {                                 // Not logged into your webpage or we are unable to tell.
            console.log("user not conneted");
        }
    }

    window.fbAsyncInit = function () {
        FB.init({
            appId: '{your-app-id}',
            cookie: true,
            xfbml: true,
            version: '{api-version}'
        });

        FB.getLoginStatus(function (response) {   // Called after the JS SDK has been initialized.
            statusChangeCallback(response);        // Returns the login status.
        });
    };

    (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 = "https://connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>   
于 2020-05-05T05:32:44.870 回答