我有一个页面,当用户允许该应用程序时,它将列出用户的页面喜欢,并检查用户是否喜欢我的某个 Facebook 页面并且是该 facebook 页面的成员(在某种程度上两者都是相似的) , 使用 FB.API
问题是某些用户根本没有返回任何数据,无论操作系统或浏览器如何。我已经在应用程序上设置了“user_likes”权限,但无济于事。
<html>
<head>
<title>My Facebook Login Page</title>
</head>
<body>
<div id="fb-root"></div>
<div id="lblName"></div>
<div id="lblAccessToken" style="display: none;"></div>
<br><br>
<fieldset>
<legend>Your Page Likes</legend>
<div id="lblLikes"></div>
</fieldset>
<fieldset>
<legend>Check whether you like [My Fan Page] </legend>
<div id="lblYouLike"></div>
</fieldset>
<fieldset>
<legend>Check whether you are a fan of [My Fan Page]</legend>
<div id="lblMem"></div>
</fieldset>
<div id="divLogin" style="display:none;">
<div class="fb-login-button">Login with [App]</div>
</div>
<script>
var accessToken;
var uid;
var lblName = document.getElementById("lblName")
var divLogin = document.getElementById("divLogin")
var lblAccessToken = document.getElementById("lblAccessToken")
var lblLikes = document.getElementById("lblLikes")
var lblYouLike = document.getElementById("lblYouLike")
var lblMem = document.getElementById("lblMem")
window.fbAsyncInit = function () {
FB.init({
appId: 'APP_ID', // App ID
channelUrl : 'http://www.example.com/testing/channel.html', // Channel File
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
FB.getLoginStatus(
function (response) {
checkStatus(response)
}
);
FB.Event.subscribe(
'auth.authResponseChange',
function (response) {
checkStatus(response)
}
);
FB.Event.subscribe(
'edge.create',
function (response) {
//alert('You liked the URL: ' + response);
ufCheckLike()
}
);
};
// Load the SDK Asynchronously
(
function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script');
js.id = id;
js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
} (document)
);
function checkStatus(response){
if (response.status === 'connected') {
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
lblAccessToken.innerHTML = accessToken;
FB.api(
'/me',
'get',
function(response) {
lblName.innerHTML = response.name;
}
);
divLogin.style.display = "none";
ufCheckLike()
}
else {
divLogin.style.display = "";
}
}
function ufCheckLike() {
if (isLoggedIn()) {
FB.api(
'/me/likes?' +
'access_token=' + accessToken,
'get',
function(likes) {
if (likes) {
if (likes.data && likes.data.length > 0) {
for (var i = 0; i < likes.data.length; i++) {
var like_detail = likes.data[i]
var lsHTML = like_detail.name + " : " + like_detail.id + "<br>"
if(like_detail.id == "<PAGE_ID>")
lblLikes.innerHTML = lsHTML + lblLikes.innerHTML
else
lblLikes.innerHTML += lsHTML
}
}
}
}
);
FB.api(
'/me/likes/<PAGE_ID>?' +
'access_token=' + accessToken,
'get',
function(likes) {
if (likes) {
if (likes.data && likes.data.length > 0) {
lblYouLike.innerHTML = "You Like [Fan Page]!"
}
}
}
);
FB.api(
'/<PAGE_ID>/members/' + uid + '?' +
'access_token=' + accessToken,
'get',
function(likes) {
if (likes) {
if (likes.data && likes.data.length > 0) {
lblMem.innerHTML = "You are a fan of [Fan Page]!"
}
}
}
);
}
}
function isLoggedIn() {
if (!accessToken) {
divLogin.style.display = "";
return false;
}
return true;
}
</script>
</body>
</html>
我还实现了一个 FQL,它实际上做了同样的事情来检查用户是否喜欢我的页面,并且在某些数据对象返回空行的用户上发生了同样的情况。
var query = "SELECT uid FROM page_fan where uid=me() and page_id = PAGE_ID ' + 'and access_token = " + accessToken;
//Access token is optional
var names = FB.Data.query(query);
names.wait(
function (rows) {
if (rows.length > 0) {
// liked, do codes here
}
else {
// not yet liked, show like button
}
}
);
我已经应用了此处找到的一些建议,但该脚本仍然不适用于某些用户。除了在应用程序的用户和朋友权限设置中设置“user_likes”之外,我不知道应该修改哪些特定帐户/隐私/应用程序设置以允许查询用户的页面喜欢。