2010-08-02 118 views
1

我正在爲我們的網站上的臉書連接創建登錄代碼, 但是我無法找到如何檢查用戶是否具有所需的權限。臉書連接:檢查用戶是否具有javascript的權限

與舊的JavaScript,一個對話框將打開每個權限和返回代碼會說,如果權限被接受或不,它是如何工作的JavaScript代碼?

這裏是我走到這一步的代碼,與TODO這裏我要檢查用戶是否得到了許可

<div id="fb-root"></div> 
    <script> 
    window.fbAsyncInit = function() { 
    FB.init({appId: 'MY API KEY', status: true, cookie: true,xfbml: true}); 
    FB.Event.subscribe('auth.login', function(response) { 
     alert("logged in"); 

     //TODO: check if all perms has been accepted!!!! 
     //if they have NOT been accepted, I want to logout the user 
    }); 

    FB.getLoginStatus(function(response) { 

     if (response.session) {  
      // logged in and connected user, again, check if all perms has been accepted 
      alert("already logged in");  
     } 

    }); 


    }; 
    (function() { 
    var e = document.createElement('script'); e.async = true; 
    e.src = document.location.protocol + 
     '//connect.facebook.net/en_US/all.js'; 
    document.getElementById('fb-root').appendChild(e); 
    }()); 
</script> 
<fb:login-button perms="email,user_birthday,status_update,publish_stream" >Login with Facebook</fb:login-button> 

順便說一句,在文檔中,他們有這樣的例子 http://developers.facebook.com/docs/reference/javascript/FB.login

哪裏他們使用自定義按鈕,這就是爲什麼我懷疑會有類似的fb:登錄按鈕

+0

只是問:http://stackoverflow.com/questions/3388367/check-for-extended-permissions-with-new-facebook-javascript-sdk/3388721#3388721 – serg 2010-08-02 17:38:11

+0

肯定肯定有沒有fql的方式呢? 謝謝,如果沒有別的東西出現,可能會使用fql解決方案 – JohnSmith 2010-08-02 17:57:05

回答

2
FB.Event.subscribe('auth.login',function(response) { 
    if(response.session) { //checks if session is true 
     alert('logged in'); 

     if(response.perms) { //checks if perms is true = permissions are granted 
     alert('perms granted'); 
     } 
     else { //if perms is false = no permissions granted 
     alert('no perms'); 
     } 
    } 
    else { //if something goes wrong 
     alert('login failure'); 
    } 
});

Origina l Facebook指南: http://developers.facebook.com/docs/reference/javascript/FB.login

1

我做了這個解決方案來檢查「user_friends」和「publish_actions」的權限,如果不允許這兩個,強制用戶「重新認證」。只有在給出所有權限時纔會調用回調函數。

function login(cb,forceAuth) { 
    FB.getLoginStatus(function (response) { 
     if (response.status !== 'connected' || forceAuth==true){ 
      FB.login(function (response) { 
       checkPermissions(cb,false); 
      }, {scope: 'publish_actions,user_friends'}); 
     } else { 
      checkPermissions(cb); 
     } 
    }); 
} 

function checkPermissions(cb,forceAuth){ 
    FB.api(
     "/me/permissions", 
     function (response) { 
      if (response.data[0]['publish_actions']==undefined || response.data[0]['publish_actions']==0 || 
       response.data[0]['user_friends']==undefined || response.data[0]['user_friends']==0) { 
       if (forceAuth!=false) 
        login(cb,true); 
      } else { 
       cb(); 
      } 
     } 
    ); 
} 

如何使用:

login(function() { 
    myLogedAndAllowedFunction(); 
}); 
相關問題