Javascript FB.getLoginStatus() 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7251260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FB.getLoginStatus() not working
提问by darangho
I am trying to write a code that checks whether the user is logged in or not, and found that there is a built-in method in FBJS API, which is called getLoginStatus()
我正在尝试编写一个检查用户是否登录的代码,发现FBJS API中有一个内置方法,称为getLoginStatus()
I have implemented it inside of html, but for some how, alert() inside of the getLoginStatus() is not fired.
我已经在 html 中实现了它,但是对于某些方法,getLoginStatus() 中的 alert() 没有被触发。
I have also tried to add channelUrl at init(),but it still does the same.
我也尝试在 init() 中添加 channelUrl,但它仍然是一样的。
Below is the code that I have written.
下面是我写的代码。
Can anyone help me with it?
任何人都可以帮助我吗?
Thanks in advance!
提前致谢!
<!-- Initialize FB API for use -->
<div id="fb-root"></div>
<script type="text/javascript">
//var curLoc = window.location;
//var chanURL = curLoc.protocol + "//" + curLoc.hostname + ":" +
//curLoc.port + "/channel.html"
window.fbAsyncInit = function() {
FB.init({appId: '####', status: true, cookie: true, xfbml: true});
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
alert('logged in');
} else {
// no user session available, someone you dont know
alert('not logged in');
}
});
};
(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>
回答by Jochem
I had this the other day and the problem was that I was NOT logged in in Facebook AND my App was in Sandbox mode. Maaaaybe it's the same here, longshot but wanted to suggest it anyway.
前几天我遇到了这个问题,问题是我没有登录 Facebook 并且我的应用程序处于沙盒模式。Maaaaybe 这里是一样的,远射,但无论如何还是想提出建议。
Note: Comparable combinations that wouldn't work are being logged in in Facebook as a Test user that has no access to the application in question or being logged in in Facebook as a non-test/non-admin user AND having the app in sandbox mode.
注意:以测试用户身份登录 Facebook 或以非测试/非管理员用户身份登录 Facebook 并且在沙箱中使用该应用程序,但无法使用的比较组合模式。
回答by saurabhj
BTW. They changed it again. It is now:
顺便提一句。他们又改了。就是现在:
response.authResponse
response.authResponse
and not response.sessionanymore.
而不是response.session了。
回答by Monkeystation
I was having the same problem. I was caused by the 'Sandbox Mode' in the FB App settings. When enabled, it won't fire the getLoginStatus() response. Probably because it is unknown if you're actually a registered developer when not logged in.
我遇到了同样的问题。我是由 FB 应用程序设置中的“沙盒模式”引起的。启用后,它不会触发 getLoginStatus() 响应。可能是因为不知道您在未登录时是否真的是注册开发人员。
回答by Bobbi Bennett
They changed it. Run console.log on response, and see you need to test as so:
他们改变了它。运行 console.log 响应,并看到您需要这样测试:
if (response.status=='connected')) {...}
edit: here is most of my login:
编辑:这是我的大部分登录信息:
<?php
// using Facebook PHP SDK (v.3.1.1)
$facebook = new Facebook(array(
'appId' => APP_ID
'secret' => SECRET
));
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
?>
and the js in the body:
和身体中的js:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId(); ?>',
status : true,
cookie : true,
oauth: true,
channelUrl : MY_CHANNEL_URL,
xfbml : true
});
FB.getLoginStatus(function(response) {
console.log( response );
if ((response.status)&&(response.status=='connected')) {
loadStreamInto ( $("#FBmessages") );
} else {
}
});
};
</script>
回答by thefreeman
I assume you replaced you actual appId with #### in the code you linked. If not, that is your problem.
我假设您在链接的代码中用 #### 替换了实际的 appId。如果没有,那是你的问题。
Assuming you have actually placed your appId in here, it could be related to the fact that you are testing on localhost. I believe that the javascript will not function if the url it is being served from does not match up with the url associated with the app for the given App Id
假设您实际上已将 appId 放在此处,这可能与您在 localhost 上进行测试有关。我相信如果提供它的 url 与给定 App Id 的应用程序关联的 url 不匹配,javascript 将无法运行
回答by Craig Myles
I'm not saying this would work for you, but I had a similar problem with my deployed app. It worked fine on my localhost but not when deployed to Heroku. I realised that in the Facebook App settings I had the urls all set as local ones - when I changed this to my deployed app url it fixed it.
我并不是说这对你有用,但我部署的应用程序也有类似的问题。它在我的本地主机上运行良好,但在部署到 Heroku 时就不行了。我意识到在 Facebook 应用程序设置中,我将所有网址都设置为本地网址 - 当我将其更改为部署的应用程序网址时,它修复了它。
回答by alexl
the response.status=='connected' happens only if you put oauth:true in fb.init
仅当您将 oauth:true 放入 fb.init 时,才会发生 response.status=='connected'
code looks fine to me should work as is not sure why it doesn't work
代码对我来说看起来不错应该可以工作,因为不确定为什么它不起作用