2012-05-21 35 views
0

,以檢查是否已發售者給所有需要的權限,我願意這樣做:的FB.login響應總是返回true

FB.login(function(response){ 
      console.log(response.status); 
      if (response.status == 'connected') { 
       /* user gave permssions */ 
      }else{ 
       /* user didnt, unmark the checkbox */ 
       $('input:checkbox').removeAttr('checked'); 
      } 
    }, { scope: 'publish_stream' }); 

的問題是,這是永諾返回true,它不問題,如果用戶:登錄,忽略或關閉彈出窗口。

有什麼想法爲什麼?

也試過:如果(response.authResponse){沒有成功..

+0

你是什麼意思 '返回true'?由於它是異步的,你不應該對FB.login的返回值做任何事情。 –

+0

if(response.status =='connected')its allways true。因此,即使用戶省略或關閉權限對話框請求,response.status也是「連接的」。 –

回答

2

這裏的問題是,publish_streamextended permission,這意味着用戶可以選擇退出該權限。一般來說,當用戶點擊回調代碼塊時,他們已經對您的應用程序進行了身份驗證,但未必具備您要求的所有權限,因爲其中一些權限可能爲extended permissionsresponse.status僅用於傳達用戶是否對應用程序進行了身份驗證的狀態,而不是他們是否已接受您請求的所有對話提示/權限。在你的情況下,publish_stream是一個擴展權限,所以你不能保證在你的回調中擁有該用戶的權限。如果在用戶已經通過身份驗證後,您要求publish_stream作爲增量許可,那麼您的條件檢查response.status將始終返回true(因爲根據定義,用戶已經對您的應用程序進行了身份驗證)。

如果要驗證您的回調中是否具有publish_stream權限,請在圖表api上使用/me/permissions端點檢查權限。

你需要的是這樣的:

FB.login(function(response){ 
    if (response.status == 'connected') { 
     FB.api('/me/permissions', function(response) { 
      var permsArray = response.data[0]; 
      // Permissions that are needed for the app 
      var permsNeeded = ['publish_stream']; 
      var permsToPrompt = []; 
      for (var i in permsNeeded) { 
       if (permsArray[permsNeeded[i]] == null) { 
        permsToPrompt.push(permsNeeded[i]); 
       } 
      } 

      if (permsToPrompt.length > 0) { 
       $('input:checkbox').removeAttr('checked'); 
      } 
     } 
    } else { 
     /* user didnt, unmark the checkbox */ 
     $('input:checkbox').removeAttr('checked'); 
    } 
}, { scope: 'publish_stream' }); 
0

我不知道爲什麼,但下面的代碼工作正常,至少對我來說〜

window.fbAsyncInit = function() { 
    FB.init({ 
    appId  : '<?php echo FACEBOOK_APP_ID ?>', 
    status  : true, 
    cookie  : true, 
    xfbml  : true, 
    oauth  : true, 
    }); 
FB.getLoginStatus(function(response){ 
    if (response.status === 'connected') { 
    // the user is logged in and has authenticated your 
    // app, and response.authResponse supplies 
    // the user's ID, a valid access token, a signed 
    // request, and the time the access token 
    // and signed request each expire 
    var uid = response.authResponse.userID; 
    var accessToken = response.authResponse.accessToken; 
    var signed_request = response.authResponse.signedRequest; 
    // avoid using cookie 
    self.location= "<?php echo site_url()?>/signup/fb_login/"+uid; 

    } else if (response.status === 'not_authorized') { 
    // the user is logged in to Facebook, 
    // but has not authenticated your app 
    FB.login(function(response) { 
    if (response.authResponse) { 
     self.location="<?php echo site_url()?>/signup/fb_register"; 
     /* FB.api('/me', function(response) { */ 
     /* }); */ 
    } }, {scope: 'email,user_hometown'}); 
    } else { // unknown 
    // the user isn't logged in to Facebook. 
    } 
}); 
    FB.Event.subscribe('auth.login', function(response) { 
     window.location.reload(); 
    }); 
    FB.Event.subscribe('auth.logout', function(response) { 
     window.location.reload(); 
    }); 
}; 
(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)); 

`