我有以下JS代码。
代码的目的是首先获取用户的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!!!');
}
});
答案 0 :(得分:1)
FB.getLoginStatus
检查用户是否已连接到您的应用程序..而不是Facebook。
但是当用户未连接到您的应用程序时,.status
属性可以告诉您失败的原因(如果用户根本没有登录,或只是登录到您的应用程序< / em>的)。
所以你应该使用的结构是
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。