I am trying to check if a user is logged in with my app, but I am getting a "
我正在尝试检查用户是否使用我的应用程序登录,但我收到了“
FB.getLoginStatus()
called before callingFB.init()
.在调用FB.init()之前调用FB.getLoginStatus()。
" error in the console. The setSize appears to work (although not entirely 1710 height but definately around 1500) so I cannot understand why the getLoginStatus()
gives the error.
“控制台中的错误.setSize似乎工作(虽然不完全是1710高度,但绝对是1500左右)所以我无法理解为什么getLoginStatus()会给出错误。
I also double checked with the appID (removed below) and that is definately correct. The script is included via right below my and div
我还用appID(在下面删除)进行了双重检查,这肯定是正确的。脚本包含在我和div的正下方
window.fbAsyncInit = function() {
FB.init({
appId : 'APPID', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
FB.Canvas.setSize({width: 520, height: 1710});
FB.Canvas.setAutoResize(100);
FB.getLoginStatus(function(response) {
if (response.authResponse) {
// logged in and connected user, someone you know
alert("?");
} else {
// no user session available, someone you dont know
alert("asd");
}
});
};
2 个解决方案
#1
31
You need to load the api asynchronously. Try removing your <script src="connect.facebook.net/en_US/all.js"></script>
and updating your JS to:
您需要异步加载api。尝试删除
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
//
// All your canvas and getLogin stuff here
//
};
// Load the SDK Asynchronously
(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>
Oh and check the documentation!
哦,检查文档!
#2
26
This error will also occur if you don't provide an appId to FB.init. Example:
如果您不向FB.init提供appId,也会发生此错误。例:
FB.init({
appId: ''
, channelUrl: '//domain/channel.html'
, status: true
, cookie: true
});
Will result in:
将导致:
FB.getLoginStatus() called before calling FB.init().
在调用FB.init()之前调用FB.getLoginStatus()。
#1
31
You need to load the api asynchronously. Try removing your <script src="connect.facebook.net/en_US/all.js"></script>
and updating your JS to:
您需要异步加载api。尝试删除
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
channelURL : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
//
// All your canvas and getLogin stuff here
//
};
// Load the SDK Asynchronously
(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>
Oh and check the documentation!
哦,检查文档!
#2
26
This error will also occur if you don't provide an appId to FB.init. Example:
如果您不向FB.init提供appId,也会发生此错误。例:
FB.init({
appId: ''
, channelUrl: '//domain/channel.html'
, status: true
, cookie: true
});
Will result in:
将导致:
FB.getLoginStatus() called before calling FB.init().
在调用FB.init()之前调用FB.getLoginStatus()。