Facebook API FB.getLoginStatus returns undefined
I have added a like button on my website, and want to know who clicked the like button. These are my sample code:
<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) {
// l开发者_StackOverflow中文版ogged 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>
When I clicked the Like button, and login facebook, do like successfully, but got "no user session available" alert.
So I am confused:
- My appId in FB.init is the same as fb:like tag, is it right?
- Are there something wrong in my program ?
- How to get user name who click the Like button?
Thanks in advance.
Update:
@OffBySome Thanks for your answer.
You means I must call FB authenticate API, then I could get the user's info? But...in facebook development doc FAQ:
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.
So, It means I cannot know who clicks the Like Button, just know the button is clicked.
If I want to get user's info, I must implement the login action before click Like? It's really bad userbility.
Could anyone give a good idea to resolve it...
I am pretty sure you won't get a Facebook session just by liking a page. You normally only get a user Facebook session when they authenticate with your application (for example, with <fb:login-button autologoutlink="true"></fb:login-button>
) . If do an an alert(JSON.stringify(response));
inside that function what values do you get?
Try this..
FB.Event.subscribe('edge.create', function(href, widget)
{
alert('You just liked '+href);
session = FB.getSession();
uid = session['uid'];
alert(uid);
});
精彩评论