我有以下 JS 代码。
代码的目的是首先获取用户的 facebook id,然后使用 FQL 根据我的页面 ID 检查该 id 并确保用户是粉丝。
我遇到的问题是,代码真正起作用的唯一时间是我使用自己的个人 Facebook 个人资料登录。我认为这是因为我的配置文件和 FB.init appid 以某种方式关联?
有人可以看一下这段代码并告诉我哪里出错了吗?
我的目标再次是使用 JS 首先获取用户 ID(因此是他们的缩略图),然后将其与我自己的 Facebook 页面交叉引用以检查他们是否是粉丝。如果他们是 Facebook 粉丝,那么我可能会给他们一张优惠券或其他东西。
提前致谢。
<script src="http://connect.facebook.net/en_US/all.js"></script>
//Connect to facebook with my app id..
FB.init({
appId:'135445813195028',
cookie:false,
status:true,
xfbml:true
});
//Check to see if user is actually CONNECTED???
FB.getLoginStatus(function(response) {
if (response.session) {
// USER IS CONNECTED
FB.api('/me', function(user) {
if (user != null) {
var image = document.getElementById('imagez');
image.src = 'http://graph.facebook.com/' + user.id + '/picture?type=large';
var name = document.getElementById('name');
name.innerHTML = user.name;
FBID = user.id;
console.log('Facebook ID:' + FBID);
//assemble an FQL query to see if the guy is a fan of our page...
myquery = 'SELECT uid FROM page_fan WHERE page_id = 126923080686340 AND uid = ' + FBID;
console.log('query = ' + myquery);
///Run FQL Query to see if user is a fan
FB.api({
method: 'fql.query',
query: myquery
}, function(resp) {
if (resp.length) {
var IsFan = true;
alert('You are A fan!')
console.log('Visitor is a fan');
//show coupon code...
} else {
alert('Signed into facebook, but Not a fan!');
var IsFan = false;
console.log('Visitor is not a fan');
//show like button...
//once like is clicked then refresh the page...
}
});
//END Run FQL Query to see if user is a fan
}
});
//Figure out if they are a fan...
} else {
// USER IS NOT CONNECTED
alert('NO USER session, user is not logged into facebook!!!');
}
});
最佳答案
FB.getLoginStatus
检查用户是否已连接到您的应用程序,而不是 facebook。
但是当用户没有连接到您的应用程序时,.status
属性可以告诉您失败的原因(如果用户根本没有登录,或者只是到您的应用)。
所以你应该使用的结构是
FB.getLoginStatus(function(response) {
if(response.session) {
alert('connected to Application');
} else {
// no user session available, someone you dont know
if(response.status == "notConnected") {
// But user is logged into facebook
alert("Not connected to Application, but is logged in to Facebook");
} else {
// User is not logged into facebook
alert('Not logged in to facebook');
}
}
});
但您无法访问未授权您的应用程序的用户的 ID。
不是通过 Javascript API。
关于javascript - Facebook Javascript,如何检测用户是否是我的 Facebook 页面的粉丝?在我的网站上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5493449/