2011-08-20 58 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. 
相关问题