3

ユーザーがページの読み込み時にログインしているかどうかを Web サイトに確認させようとしましたが、API の指示に従っても機能しませんでした。例を次に示します。

<!DOCTYPE html>
    <html>
        <head>
             <title>My Facebook Login Page</title>
        </head>
        <body>
            <div id="fb-root"></div>
            <script>
                alert('hello');
                window.fbAsyncInit = function() {
                    FB.init({
                        appId  : 'APP_ID',
                        status : true, 
                        cookie : true,
                        xfbml  : true,
                        oauth  : true
                    });

                    FB.getLoginStatus(function( response ) {
                       console.log(response);
                       alert('hello2');  
                    });

                    FB.Event.subscribe('auth.statusChange', function(response) {
                       alert('hello2');
                    });

                    FB.Event.unsubscribe('auth.statusChange');
               };
               (function(d){
                   var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
                   js = d.createElement('script'); js.id = id; js.async = true;
                   js.src = "//connect.facebook.net/en_US/all.js";
                   d.getElementsByTagName('head')[0].appendChild(js);
               }(document));
           </script>
       </body>
    </html>

ユーザーがログインしていない限り、アラートがヒットすることさえありません。ログアウトしている場合、このアラートまたはそのコードブロック内にあるものはトリガーされません。

4

1 に答える 1

0

上記の例では、末尾のコンマを削除します (oauth: true,) // これは単にコピー/貼り付けによるものですが、落とし穴を排除したい

デバッグには、コンソール ロギングが私の好みの方法ですが、アラートも表示されないため、状況は少しトリッキーです。

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

FB.Event.unsubscribe("auth.statusChange");
// If you were to get subscribe to work, think about unsubscribing at some point to, since most likely that's only for an initial check

または、イベント サブスクリプションの代わりに getLoginStatus を使用してみてください。

FB.getLoginStatus(function( response ) {
    console.log(response);  
});

応答はオブジェクトを提供する必要がresponse.statusあり、確認できます。可能な値は次のとおりです。connected、not_authorized、またはunknown response.authResponseには、accessToken、expiresInなどのプロパティが含まれます.

于 2012-05-01T21:41:29.243 に答える