javascript 检查用户是否喜欢 Facebook 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18918506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if the user likes the facebook page or not
提问by Mohammed Fayez Abulkas
I'm trying to check if the user liked my page before use my app on it with the following code
我正在尝试使用以下代码检查用户是否喜欢我的页面,然后再使用我的应用程序
<body onload="liked()">
<script type="text/javascript">
function liked() {
FB.api("me/likes/2002200279160150349", function(response) {
if (response.data.length == 1) {
alert("page liked already");
} else {
alert("page is NOT liked ");
}
});
}
</script>
</body>
determine that the user is authnticated in another page and logged in properly
确定用户已在另一个页面中进行身份验证并正确登录
回答by Augustus Francis
Simple Approach use this method
简单方法使用此方法
FB.api({
method: "pages.isFan",
page_id: my_page_id,
}, function(response) {
console.log(response);
if(response){
alert('You Likey');
} else {
alert('You not Likey :(');
}
}
);
But,This code only works if the user has granted an extended permission
for that which is not ideal.
但是,此代码仅在用户extended permission
为不理想的情况授予了一个时才有效。
In a nutshell, if you turn on the "OAuth 2.0 for Canvas" advanced option, Facebook will send a $_REQUEST['signed_request'] along with every page requested within your tab app. If you parse that signed_request you can get some info about the user including if they've liked the page or not.
简而言之,如果您打开“OAuth 2.0 for Canvas”高级选项,Facebook 将发送 $_REQUEST['signed_request'] 以及您的标签应用中请求的每个页面。如果您解析该 signed_request,您可以获得有关用户的一些信息,包括他们是否喜欢该页面。
function parsePageSignedRequest() {
if (isset($_REQUEST['signed_request'])) {
$encoded_sig = null;
$payload = null;
list($encoded_sig, $payload) = explode('.', $_REQUEST['signed_request'], 2);
$sig = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = json_decode(base64_decode(strtr($payload, '-_', '+/'), true));
return $data;
}
return false;
}
if($signed_request = parsePageSignedRequest()) {
if($signed_request->page->liked) {
echo "This content is for Fans only!";
} else {
echo "Please click on the Like button to view this tab!";
}
}