2011-11-14 158 views
0

我有一個很難得到Facebook的JS API來爲http://developers.facebook.com/docs/reference/javascript/的Facebook要求用戶登錄,即使他們已經登錄

具體描述工作,用戶似乎得到要求登錄即使他們已經登錄到Facebook。對於這個我做了以下內容:

<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : '3120012412', // App ID goes here 
     channelURL : '//WWW.qy.ORG/channel.php', // Channel File 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     oauth  : true, // enable OAuth 2.0 
     xfbml  : true // parse XFBML 
    }); 

    // Additional initialization code here 
    FB.login(function(response) { 
    if (response.authResponse) { 
     alert('Welcome! Fetching your information.... '); 
     FB.api('/me', function(response) { 
     alert('Good to see you, ' + response.name + '.'); 
     alert('response' + response); 

     FB.logout(function(response) { 
     alert('Logged out.'); 
     }); 
    }); 
    } else { 
     alert('User cancelled login or did not fully authorize.'); 
    } 
}, {scope: 'email'}); 
    }; 

    // Load the SDK Asynchronously 
    (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> 

我也試過:

FB.getLoginStatus(function(response) { 
    if (response.authResponse) { 
    // logged in and connected user, someone you know 
    } else { 
    // no user session available, someone you dont know 
    } 
}); 

,當我在Facebook的我已經登錄這也失敗均勻。

如果我想讓用戶通過最低限度的麻煩,只需基本訪問公共信息,該怎麼辦?我明白他們必須授權基本訪問權限,但是不應該一再詢問他們是否應該這樣做?

回答

0

您的第一個代碼塊正在將用戶登錄,然後立即再次將其註銷 - 因此,如果您已經運行了幾次,您可能未登錄。刪除FB.logout塊並再次嘗試,看看它是否有所作爲。

要記住的另一件事(可能是你的第二個例子不工作的原因)是雖然你登錄到facebook.com,你必須通過FB.login登錄到你自己的域名。 getLoginStatus將檢查您的fbsr_cookie並考慮您是否登錄,但您必須已經擁有該cookie,首先請撥打FB.login

因此,我建議使用這樣的:

// Additional initialization code here 
    FB.login(function(response) { 
     if (response.authResponse) { 
      alert('Welcome! Fetching your information.... '); 
      FB.api('/me', function(response) { 
      alert('Good to see you, ' + response.name + '.'); 
      alert('response' + response); 
      }); 
     } else { 
      alert('User cancelled login or did not fully authorize.'); 
     } 
    }, {scope: 'email'}); 
+0

非常感謝您的幫助! –

+0

這是否解決了您的問題?如果有答案,請接受答案,否則請留下更多信息。 :) – Abby

相關問題