我在这里使用 Facebook登录按钮插件和 javascript sdk
我可以使用上面的方式成功登录和注销。
当第一次使用身份验证过程时,我需要存储用户基本信息,即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>
答案 0 :(得分:14)
Subscribe to the event auth.login
。如果你这样做,Facebook会在登录后调用你的处理程序。
在该处理程序中,use FB.api
调用Graph API以获取所需的任何信息。例如,如第二个示例中所示调用/me
将获得有关登录用户的基本信息。
现在您拥有JavaScript中的所有数据。要将其发送到您的服务器,请执行一个简单的旧XMLHttpRequest / AJAX请求。您的JavaScript库可能很容易实现 - 在jQuery中这是jQuery.ajax() - 但最糟糕的情况是use XHR directly。
现在您拥有服务器上的数据,您可以随心所欲地将数据存储在数据库中。如果您只想存储一次数据,请检查您是否尚未存储有关该用户ID的信息。
答案 1 :(得分: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;
}
}
}
?>
答案 2 :(得分:3)
该解决方案存在漏洞 - 这意味着用户可以编制他想要的任何信息并将XHR发回我的服务器。服务器需要直接与Facebook核对。
答案 3 :(得分:0)
//very simple just change this line
fb:login-button autologoutlink="true"
//with this one
fb:login-button autologoutlink =“true”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.