2012-04-30 126 views
0

我使用FB登錄我的演示站點連接: 我用這個代碼:Facebook登錄連接狀態

window.fbAsyncInit = function() { 
     try{ 
     FB.init({ 
      apiKey: '<MY APP KEY>', 
      channelUrl : 'www.google.com' 
     }); 

     FB.getLoginStatus(function(response){ 
      loginHandler(); 
     }); 
     }catch(error){} 
    }; 
    function loginHandler(success,fail) 
    { 

     try{ 
      if(success){ 
       alert('i am if'); 
       FB.api('/me', function(response,fail) { 
        alert("first name == " + response.first_name + "\n ln === " + response.last_name); 
       }); 
      } 
     }catch(error){} 
    } 

    $('#login').bind('click', function(response) { 
     try{ 
      FB.login(loginHandler, {scope: 'email,user_likes'}); 
     }catch(error){} 
    }); 

    $('#logout').bind('click', function() { 
     try{ 
      FB.logout(function(response){}); 
     }catch(error){} 
     }); 

現在我的問題是,警報(「我如果」);顯示每次我是否通過正確憑據facebook或不和警報(「first name ==」+ response.first_name +「\ n ln ===」+ response.last_name);顯示undefined當用戶通過時錯誤憑據並顯示用戶回來時傳遞的正確信息正確信息

一個可能的選擇是檢查像if(response.first_name!=undefined){blah blah....}但它是檢查驗證的正確方法嗎?

請一些線索....

回答

1

嘗試是這樣的:

 
//for login 
    $("#login").bind("click", function(e) {  
     var fbScope = "email,user_likes"; 
     FB.login(function(response) {   
      if(response.status == "connected"){ 
       console.log("Logged in"); 
      } 
     },{scope: fbScope}); 
    }); 
    //for checking logged in status 
    FB.getLoginStatus(function(response){ 
     if (response.authResponse) {  
      var accessToken = response.authResponse.accessToken; 
      FB.api('/me', function(response) { 
       console.log('I m still loggedin'); 
      });   
     } 
    }); 
    //for logout 
    $("#logout").live("click", function(e) {   
     FB.getLoginStatus(function(response){   
      if(response.status == "connected"){ 
       FB.logout(function(response) { 
        console.log("I m logged out now");     
       }); 
      } else { 
       console.log("Not Logged logged in"); 
      } 
     }); 
    }); 
+0

它爲我工作,謝謝....非常感謝 –

1

FB.loginFB.api沒有向您提供成功/失敗的報告,就像你想實現它。有回調沒有第二個參數...

FB.login回調得到一個和唯一的參數:

響應來自FB.getLoginStatusFB.loginFB.logout。此響應對象包含:

狀態
用戶的狀態。其中一個連接,未授權或未知。
authResponse
authResponse對象。

FB.api回調得到唯一參數是從圖形API響應通常(但不總是)含有data屬性,對象字段,布爾假或error對象(messagetypecode屬性)


您可以修改您的loginHandler,通過糾正回覆回覆來照顧正確的參數:

function loginHandler(loginResponse){ 
    try{ 
    if(loginResponse.authResponse){ 
     alert('i am if'); 
     FB.api('/me', function(response) { 
     alert("first name == " + response.first_name + 
       "\n ln === " + response.last_name); 
     }); 
    } 
    }catch(error){} 
} 
+0

+1的信息... 但我們怎麼能檢查相同..............? –

+0

@ ankur20us,我會在一分鐘內提供一些細節;) –

+0

好的我正在等待您的回覆 –