当然,您刚刚将范围user_likes添加到 facebook 登录按钮。
您可以在此处阅读有关 facebook 登录按钮和范围属性的更多信息
现在您可以访问用户的喜欢,您可以检查用户是否喜欢您的页面,如果喜欢,只需将用户重定向到您的网站,如果不是,只需提示他喜欢您的页面,然后他才能访问您的其余部分网站。
这是一些代码,使用 Facebook Javascript SDK
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'YOUR_APP_ID', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.Event.subscribe('auth.statusChange', handleStatusChange);
FB.Event.subscribe('edge.create', like);
};
// 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/pt_PT/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
然后在页面底部,放上这段javascript代码
<script>
//this function is fired whenever the user presses the like button
function like(response) {
if(response="https://www.facebook.com/YOUR_PAGE_USERNAME")
//do something when the user likes your page, like redirect him, or something like that
}
//this function is fired everytime the page is loaded
function handleStatusChange(response) {
if (response.authResponse) {
updateUserInfo(response);
}
}
//this function is called by the function handleStatusChange and it checks everytime the page is loaded if the user already likes your page
function updateUserInfo(response) {
token = response.authResponse.accessToken;
FB.api('/me/likes/YOUR_PAGE_ID', function(response) {
if(response.data.length){
//the user already likes your page
}
else{
//the user doesn'y like your page, or the user hasn't granted permission for you to access his likes. show him the like button
}
}, {access_token: token});
}
</script>