我有以下的JS代码。Facebook Javascript,如何检测用户是否是我的Facebook页面的粉丝?在我的网站上?
该代码的目的是首先获取用户的Facebook ID,然后使用FQL检查该ID与我的页面ID并确保用户是粉丝。
我遇到的问题是代码实际工作的唯一时间是如果我用我自己的个人脸谱配置文件登录。我认为它是因为我的个人资料和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!!!');
}
});
我不能肯定地说,但我认为这是一个权限问题。 Facebook可能不希望您能够在没有权限的情况下(例如通过应用程序)检查用户的页面。 – n00dle 2011-03-30 22:56:09
希望你不能。这不是你的事情。 – 2011-03-30 22:58:08
不能说FB是否允许这样做,但是您确定其他人或其他人尝试启用控制台吗?否则你的'console.log'命令会失败并停止JS。 – 2011-03-30 22:59:12