Haf*_*fiz 16 php facebook facebook-login
我正在使用js连接fb.这是我的代码:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxxxx',
status : true,
cookie : true,
xfbml : true,
oauth : true
});
FB.Event.subscribe('auth.login', function(response) {
obj=response.authResponse;
token=obj.accessToken;
setCookie("access_token_fb", token, '2');
window.location.reload();
},true);
};
(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));
</script>
Run Code Online (Sandbox Code Playgroud)
登录我正在使用:
<div class="fb-login-button" scope="email,user_checkins" >Login with Facebook</div>
Run Code Online (Sandbox Code Playgroud)
订阅功能中的'auth.login'有一个响应处理程序,只有当用户点击fb按钮并且尚未登录FB时才会调用它,在fb窗口中输入他/她的电子邮件地址和密码.如果用户已登录facebook,则fb对话框打开和关闭.然后它不会将控制传递给响应者.我读到某处force ="true"使其能够检测状态变化并调用回调函数.但我不知道在哪里添加力量.我认为它应该在登录div中.它是真的还是有其他方法?
谢谢你的时间.
Jai*_*dya 12
方法1:
虽然我相信DMCS已经为这个问题提供了正确的解决方案,但这是另一种方法.将FB.login方法与自定义登录链接一起使用.
<script>
window.fbAsyncInit = function() {
FB.init({
appId: 'YOUR-APP-ID',
status: true,
cookie: true,
xfbml: true,
oauth: true
});
};
function facebookLogin() {
FB.login(function(response) {
if (response.authResponse) {
console.log('Authenticated!');
// location.reload(); //or do whatever you want
} else {
console.log('User cancelled login or did not fully authorize.');
}
},
{
scope: 'email,user_checkins'
});
}
(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));
</script>
Run Code Online (Sandbox Code Playgroud)
身体标记:
<div id='fb-root></div><!-- required for SDK initialization -->
<a id='fb-login' href='#' onclick='facebookLogin()'>Login with Facebook</a>
Run Code Online (Sandbox Code Playgroud)
当您单击该按钮时,它将在FB.login中运行回调函数.
方法2:
或者,使用FB登录按钮插件,您可以订阅auth.statusChange并检查用户的状态作为响应.
FB.Event.subscribe('auth.statusChange', function(response) {
// check for status in the response
});
Run Code Online (Sandbox Code Playgroud)
请注意,如果您使用的是fb登录按钮插件,并且用户已登录并已连接到该应用程序,则不会显示登录按钮(请在此处的文档中阅读)
我建议让Facebook为你做繁重的工作.请注意一些变化:1.指定channelUrl,2.使用FB.getLoginStatus 3.删除设置cookie.4.改变要收听的事件(所以如果他们在不同的窗口中注销,或者在其他地方将你的应用程序deauth等)
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxxxx',
status : true,
cookie : true,
xfbml : true,
oauth : true,
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html' // Channel File
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
// Do something with the access token
} else {
// Subscribe to the event 'auth.authResponseChange' and wait for the user to autenticate
FB.Event.subscribe('auth.authResponseChange', function(response) {
// nothing more needed than to reload the page
window.location.reload();
},true);
// now dynamically show the login plugin
$('#mydiv').show();
}
});
};
(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));
</script>
Run Code Online (Sandbox Code Playgroud)