response.status === 'not authorized'
「else if」の場合と「else」の場合 (「不明」、つまり、ユーザーが facebook にログインしていない) が実行されていないように見えるのはなぜでしょうか。アプリにログインすると、それに応じて testAPI() が呼び出されresponse.stats ===
'connected'
ます。ただし、Chrome でシークレット モードを開くと、他のケースの testAPI() 関数が呼び出されません。理由を知っている人はいますか?
window.fbAsyncInit = function() {
FB.init({
appId : 'appnumberhere', // App ID
channelUrl : '//localhost/appname/channel.php', // 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.authResponseChange', function(response) {
if (response.status === 'connected') {
testAPI(); // works
} else if (response.status === 'not_authorized') {
testAPI(); // not called. Nothing in console.
FB.login();
} else {
testAPI(); // not called. Nothing in console.
FB.login();
}
});
}); // end async init function
testAPI 関数:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
}
補足として、ログインしていない場合、Facebookのログインダイアログがポップアップ表示されません。これは、 fb.login() が呼び出すと思われるものです。
余談
また、奇妙なことに、アプリが機能<script src="//connect.facebook.net/en_US/all.js"></script>
するには、FB SDK インクルードの下<script>(function(d){ var js,....</script>
とチャネル ファイル内に含める必要があります。そうしないと、一部のパーツがロードされません。これが関係しているかどうかは定かではありませんが、非常に奇妙です。