我在我的网站上添加了一个“赞”按钮,并且想知道谁点击了“赞”按钮。 这些是我的示例代码:
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: '182722795115444', status: true, cookie: true,
xfbml: true});
// To find who clicked the like button
FB.Event.subscribe('edge.create', function(response){
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
alert("logged in and connected user");
} else {
// no user session available, someone you dont know
alert("no user session available");
}
});
});
};
(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>
<script src="http://connect.facebook.net/en_US/all.js#appId=182722795115444&xfbml=1"></script>
<fb:like href="www.blogcountry.net" send="true" width="450" show_faces="true" font=""></fb:like>
当我点击“Like”按钮并登录 facebook 时,成功点赞,但收到“没有可用的用户 session ”警报。
所以我很困惑:
- 我在 FB.init 中的 appId 与 fb:类似标签,对吗?
- 我的方法有问题吗? 程序?
- 如何获取点击的用户名 喜欢按钮?
提前致谢。
更新:
@OffBySome 感谢您的回答。
你的意思是我必须调用FB认证API,然后我才能获取用户的信息? 但是...在 facebook development doc常见问题解答:
How do I know when a user clicks a Like button?
If you are using the XFBML version of the button, you can subscribe to the 'edge.create' event through FB.Event.subscribe.
所以,这意味着我无法知道谁点击了“赞”按钮,只知道该按钮被点击了。
如果我想获取用户的信息,我必须在点击“赞”之前执行登录操作吗? 这确实是糟糕的用户体验。
谁能给出解决这个问题的好主意......
最佳答案
我很确定您不会仅仅通过喜欢某个页面来获得 Facebook session 。通常,只有当用户通过您的应用程序进行身份验证时(例如,使用 <fb:login-button autologoutlink="true"></fb:login-button>
),您才会获得用户 Facebook session 。如果做一个 alert(JSON.stringify(response));
在该函数中你得到什么值?
关于javascript - Facebook API FB.getLoginStatus 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6414105/