2010-08-02 117 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(); 
}); 
相关问题