確かに、スコープuser_likesを facebook ログイン ボタンに追加しただけです。
Facebook のログイン ボタンとスコープ属性の詳細については、こちらを参照してください。
ユーザーのいいねにアクセスできるようになったので、ユーザーがあなたのページを気に入っているかどうかを確認できます。 Webサイト。
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>