2011-08-20 57 views
13

這裏我使用Facebook的登錄按鈕插件的JavaScript SDK使用Facebook登錄按鈕插件獲取用戶基本信息?

我能夠通過使用上述成功登錄和註銷。

當第一次用戶通過驗證過程時,我需要將用戶基本信息(即Facebook登錄名,電子郵件)存儲在我的數據庫中。

請建議我如何做到這一點。

<p><fb:login-button autologoutlink="true"></fb:login-button></p> 


    <div id="fb-root"></div> 
    <script> 
     window.fbAsyncInit = function() { 
      FB.init({ appId: '123456', status: true, cookie: true, 
       xfbml: true 
      }); 
     }; 


     (function() { 
      var e = document.createElement('script'); 
      e.type = 'text/javascript'; 
      e.src = document.location.protocol + 
      '//connect.facebook.net/en_US/all.js'; 
      e.async = true; 
      document.getElementById('fb-root').appendChild(e); 
     }()); 
    </script> 

回答

14

Subscribe to the eventauth.login。如果你這樣做,Facebook會在發生登錄後打電話給你的處理程序。

在該處理程序中,use FB.api可以調用圖形API來獲取任何您想要的信息。例如,如第二個示例中所示,調用/me將爲您提供有關已登錄用戶的基本信息。

現在您擁有所有JavaScript數據。要發送到您的服務器,請執行簡單的舊XMLHttpRequest/AJAX請求。您的JavaScript庫可能會使這個很簡單 - 在jQuery中這是jQuery.ajax() - 但最糟糕的情況是您可以use XHR directly

現在你的服務器上有數據,你可以做任何你想做的事情,比如將它存儲在數據庫中。如果您只想存儲一次數據,請檢查是否尚未存儲有關該用戶標識的信息。

+10

Per @Jon Watte的回答,請注意,通過AJAX以這種方式發送的用戶信息c不被信任。最好只發送Facebook用戶ID和訪問令牌到服務器,並在服務器端API調用中查找用戶信息。 – grossvogel

+0

直接與Facebook沒有更優雅的方式嗎?這個解決方案非常不安全 – basZero

3

該解決方案存在漏洞 - 這意味着用戶可以編輯他想要的任何信息並將XHR發佈回我的服務器。服務器將需要直接與Facebook進行檢查。

+0

這應該是一個評論,所以我在上面的答案中加了一個。 – grossvogel

+0

大多數網站使用FB登錄或手動表單登錄。 FB登錄如此安全沒有意義,如果他們想要做的只是填滿東西,他們可以使用手動表單登錄。沒有任何意義,在一個而不是其他的安全。要記住的東西 - 你永遠不會比你最薄弱的環節更安全。 –

5

也可以使用PHP SDK和JS SDK的組合,後者執行登錄,前者將數據存儲在服務器上。喜歡的東西:

<?php 
require_once 'config.php'; 
require_once 'lib/facebook.php'; 

$facebook = new Facebook(array(
    'appId' => FB_APP_ID, 
    'secret' => FB_APP_SECRET, 
)); 


?> 
<!DOCTYPE html> 
<html xmlns:fb="http://www.facebook.com/2008/fbml"> 
<body> 

<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId:'<?php echo $facebook->getAppID() ?>', 
      cookie:true, 
      xfbml:true, 
      oauth:true 
     }); 
     FB.Event.subscribe('auth.login', function (response) { 
      window.location = "showUser.php"; //redirect to showUser.php on Login 
     }); 
     FB.Event.subscribe('auth.logout', function (response) { 
      window.location.reload(); 
     }); 
    }; 
    (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> 
<div class="fb-login-button" data-show-faces="true" data-width="200" 
    data-max-rows="1"></div> 
</body> 
</html> 

而且在showUser.php你有這樣的:

<?php 
#showUser.php 
require_once 'config.php'; 
require_once 'lib/facebook.php'; 

$facebook = new Facebook(array(
    'appId' => FB_APP_ID, 
    'secret' => FB_APP_SECRET, 
)); 

$user = $facebook->getUser(); 

if($user) 
{ 
    if ($user) { 
     try { 
      // Proceed knowing you have a logged in user who's authenticated. 
      $user_profile = $facebook->api('/me'); 
      var_dump($user_profile); //You can now save this data 
     } catch (FacebookApiException $e) { 
      echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>'; 
      $user = null; 
     } 
    } 
} 

?> 
+0

這裏引用的config.php文件是什麼? App ID和密鑰已經包含在內。正下方的包含。 –

+0

不,包含我下面的App ID和密鑰傳遞給Facebook的構造函數。它們在config.php中定義 – JustB

0
//very simple just change this line 
fb:login-button autologoutlink="true" 

//with this one 

FB:登錄按鈕autologoutlink = 「真」 onlogin = 'your_ajax_fun_that_store_in_db()'

function your_ajax_fun_that_store_in_db(){ 
    FB.api('/me', function(response) { 
    $.post("ajax/store_user_info.php",response, function(data) { 
    //write you js code here ! 
    //you can use the (response) from facebook directly in your store_user_info.php as it will be sent in POST array 
    }); 
}); 
} 
//last thing when you face such a problem the first thing to do is to go back to facebook reference of fun. 
相關問題