我正在尝试在my website中集成fb登录。 点击按钮如果没有登录fb,它会询问fb凭证,然后什么都不做。 它只是向强制302状态
发送邮件请求到facebook / connect / ping这是代码
<script>
var isLoaded=false;
window.fbAsyncInit = function() {
FB.init({
appId : '{{app_id}}',
status : true,
xfbml : true
});
};
isLoaded=true;
function checkIfLoaded() {
if(isLoaded) console.log("LOADED!");
else console.log("NOT YET!");
return false;
}
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";js.async=true;
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.status === 'connected') {
onLogin(response);
} else if (response.status === 'not_authorized') {
FB.login();
} else {
FB.login();
}
});
function onLogin(response) {
console.log('login succ');
var status=response.status;
var accessToken=response.authResponse.accessToken;
var expiresIn=response.authResponse.expiresIn;
var signedRequest=response.authResponse.signedRequest;
var userID=response.authResponse.userID;
$("#status").val(status);
$("#accessToken").val(accessToken);
$("expiresIn").val(expiresIn);
$("signedRequest").val(signedRequest);
$("userID").val(userID);
$("#form").submit();
}
</script>
<a href="#" onclick="checkIfLoaded();">Check</a>
<h2>User</h2>
<div class="fb-login-button" data-max-rows="1" data-size="medium" data-show-faces="false" data-auto-logout-link="false"></div>
在我的应用仪表板中,我刚刚添加了一个平台,其网站名称如上所述。没有其他配置。
答案 0 :(得分:0)
问题是FB.Event
未订阅,因为当您尝试订阅此事件时FB
未初始化。请参阅控制台了解错误,您将看到:
FB未定义
原因是FB对象是在window.fbAsyncInit
下异步定义的,因此每当您想要进行API调用或使用FB
时,都必须在FB
定义后完成
因此,问题的解决方案是,在定义FB.event
之后(而不是在定义之前)编写FB
,就像这样 -
window.fbAsyncInit = function() {
FB.init({
appId : '{{app_id}}',
status : true,
xfbml : true
});
// write all your "FB" code here, since the FB is defined now
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.status === 'connected') {
onLogin(response);
} else if (response.status === 'not_authorized') {
FB.login();
} else {
FB.login();
}
});
};