2

Railscast に従って、「Facebook でサインイン」機能をサイトに追加しました。ログインに問題はありません。しかし、ログアウトしようとすると、Facebook 開発者アプリの設定でサンドボックス モードを無効にしても FB.getLoginStatus が発生しなかったようです (他のディスカッションで提案されているように)。

(function() {
  jQuery(function() {
    $('body').prepend('<div id="fb-root"></div>');
return $.ajax({
  url: "" + window.location.protocol + "//connect.facebook.net/en_US/all.js",
  dataType: 'script',
  cache: true
});
});

window.fbAsyncInit = function() {
FB.init({
  appId: 'xxxxxomittedxxxxx',
  cookie: true,
  status: true,
  xfbml: true,
  oauth: true
    });
$('#sign_in').click(function(e) {
  e.preventDefault();
  return FB.login(function(response) {
    if (response.authResponse) {
      return window.location = '/auth/facebook/callback';
    }
  });
});
return $('#sign_out').click(function(e) {
  FB.getLoginStatus(function(response) {
    if (response.authResponse) {
      return FB.logout(response.authResponse);
    }
  });
  return true;
});
 };

}).call(this);

FB.getLoginStatus が入らない (または機能しない) ことを私が知っている理由は、本文を次のように置き換えることです。

  FB.getLoginStatus(function(response) {
    return alert("I am here!");
  });

「sign_out」をクリックしている間、アラートが表示されません。

Chrome と Firefox の両方を同じ動作で実行しています。私が欠けているものを見つけるのを手伝ってくれる人はいますか? どうもありがとう。

私が遭遇した「行動」について、より具体的に説明しましょう。

  1. mysite.com から Facebook で初めてサインインすると、Facebook のログイン ウィンドウがポップアップして電子メールとパスワードを要求されますが、サイトに完全にサインインでき、期待どおりに動作します
  2. 次に、mysite.com/users/1 から sign_out ボタンをクリックすると、正常にサインアウトできるように見えます。
  3. 次に、mysite.com から Facebook で再度サインインすると、Facebook ログイン ウィンドウがポップアップ表示されなくなり、電子メールとパスワードを要求せずに直接 mysite.com/users/1 にログインするようになりました。
  4. 別のブラウザー ウィンドウを開いて facebook.com にアクセスし、そこからログアウトすると、mysite.com から Facebook でサインインすると、Facebook のログイン ウィンドウがポップアップし、メール アドレスとパスワードを要求されます。

「mysite.com/users/n からログアウトし、mysite.com から Facebook で再度サインインすると、Facebook のログイン ウィンドウがポップアップします」

誰でも役に立ちますか?どうもありがとう。

編集:

さらに調査した結果、「根本的な」原因はまだある可能性があることがわかりました。サインアウトはサインイン ルートの別のルート (またはページ) の下にあり、FB.getLoginStatus は mysite.com/signout の下で発生することはできません。firebug からのエラー メッセージは、「指定された URL はアプリケーションの構成で許可されていません。指定された URL の 1 つ以上がアプリの設定で許可されていません。Web サイトの URL またはキャンバスの URL と一致する必要があります。または、ドメインがアプリのドメインの 1 つのサブドメイン。」

それがルートの問題であることを証明するために、「FacebookログインのあるWebサイト」で指定されているルートルートmysite.comであるサインインルートと同じルート(ページ)にサインアウトリンクを配置しました。すべてが機能し、次のようにログアウトできます期待される:

<%= link_to "sign out facebook", "#" , id: "sign_out" %>

ちなみに、sign_out js は FB.logout(response.authResponse) uncaught [object Object] エラーを取り除くように修正されています。これは、FB.logout がパラメーターとして関数を想定しているためです。

return $('#sign_out').click(function(e) {
  FB.getLoginStatus(function(response) {
    if (response.authResponse) {
      FB.logout();
    }
  }, true);
});
};

要するに、FB.getLoginStatus には、サインイン ルートとは異なるルートからの呼び出しを処理できないバグがまだ残っている可能性があります。(私は Chrome、Firefox、および Safari でテストしましたが、すべて同じように動作しますが、IE10 には当てはまりません。どういうわけか、IE10 は別のルートでサインアウトしても動作します。)

同様の問題を抱えている人々からのコメントはありますか?お知らせ下さい。事前にどうもありがとうございました。

4

1 に答える 1

0

FB dev doc に記載されているように、getLoginStatus の 2 番目のパラメーターとして true を追加してみてください。

FB.getLoginStatus(function(response) {
  // this will be called when the roundtrip to Facebook has completed
}, true);

これにより、キャッシュが回避されます。もう 1 つのオプションは、イベントをサブスクライブすることです。

FB.Event.subscribe('auth.login', function(response) {
  // do something with response
});

全部ここから

質問があればコメントしてください。

編集:

スクリプトを少し修正し、不要なコード部分を削除しました。不要な返品が多すぎます。この変更されたスクリプト内でサインアウトしようとしましたが、必要に応じて機能します。イベントのサブスクリプションはチェック目的です。

<head>
    <title>Exam entry</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</head>
<body>
    <div id="fb-root"></div>

    <input type="button" value="Sign in" id="sign_in" />
    <input type="button" value="Sign out" id="sign_out" />

    <script type="text/javascript">
        window.fbAsyncInit = function () {
            FB.init({
                appId: '586844044669652',
                cookie: true,
                status: true,
                xfbml: true
            });


            // Here we subscribe to the auth.authResponseChange JavaScript event. This event is fired
            // for any authentication related change, such as login, logout or session refresh. This means that
            // whenever someone who was previously logged out tries to log in again, the correct case below 
            // will be handled. 
            FB.Event.subscribe('auth.authResponseChange', function (response) {
                // Here we specify what we do with the response anytime this event occurs. 
                if (response.status === 'connected') {
                    // The response object is returned with a status field that lets the app know the current
                    // login status of the person. In this case, we're handling the situation where they 
                    // have logged in to the app.
                    testAPI();
                } /*else if (response.status === 'not_authorized') {
                    // In this case, the person is logged into Facebook, but not into the app, so we call
                    // FB.login() to prompt them to do so. 
                    // In real-life usage, you wouldn't want to immediately prompt someone to login 
                    // like this, for two reasons:
                    // (1) JavaScript created popup windows are blocked by most browsers unless they 
                    // result from direct interaction from people using the app (such as a mouse click)
                    // (2) it is a bad experience to be continually prompted to login upon page load.
                    FB.login();
                } else {
                    // In this case, the person is not logged into Facebook, so we call the login() 
                    // function to prompt them to do so. Note that at this stage there is no indication
                    // of whether they are logged into the app. If they aren't then they'll see the Login
                    // dialog right after they log in to Facebook. 
                    // The same caveats as above apply to the FB.login() call here.
                    FB.login();
                }*/
            });


            $('#sign_in').click(function (e) {
                e.preventDefault();
                FB.login(function (response) {
                    if (response.authResponse) {
                        //return window.location = '/auth/facebook/callback';
                    }
                });
            });
            $('#sign_out').click(function (e) {
                FB.logout(function (response) {
                    console.log("Here logout response", response);
                });
            });

        };

        // Here we run a very simple test of the Graph API after login is successful. 
        // This testAPI() function is only called in those cases. 
        function testAPI() {
            console.log('Welcome!  Fetching your information.... ');
            FB.api('/me', function (response) {
                console.log('Good to see you, ' + response.name + '.');
            });
        }

        // Load the SDK asynchronously
        (function (d) {
            var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
            if (d.getElementById(id)) { return; }
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            ref.parentNode.insertBefore(js, ref);
        }(document));
    </script>
</body>
于 2013-07-04T22:11:27.790 に答える