如何使用Facebook JS SDK检查用户是否喜欢我的页面

how to check whether user like my page or not using facebook JS SDK?

本文关键字:喜欢 是否 我的 用户 检查 何使用 Facebook JS SDK      更新时间:2023-09-26

我目前正在一个Facebook应用程序中工作,该应用程序将在Facebook之外的一个网页中运行,具有类似页面的功能,但是基于我喜欢,我必须在不同的页面上重定向页面,

我尝试了很多代码来检查用户是否已经喜欢页面,但失败了。

因此,如果有人知道如何检查这一点,请帮助我。

提前谢谢。

注意:只想使用 JS SDK 而不是 PHP SDK。

要访问Facebook的类似数据,您需要user_likes权限。

<div id="fb-root">
</div>
var page_id = 'YOUR PAGE / URL ID';
(function() {
    var e = document.createElement('script');
    e.type = 'text/javascript';
    e.src = document.location.protocol +
                    '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
} ());
window.fbAsyncInit = function() {
    FB.init({ appId: 'YOUR APP ID', status: true, cookie: true, xfbml: true, oauth: true }); 
    FB.login(function(response) {
        if (response.authResponse) {
            FB.api('/me/likes/' + page_id, function(api_response) {
                try {
                    if ((api_response.data[0].name) != undefined)
                        alert(api_response.data[0].name);
                    else
                        alert('you are not like this page');
                }
                catch (e) {
                    alert('you are not like this page');
                }
            });
        }
    }, { scope: 'email,user_likes' });
};

必须获取user_likes权限,然后调用图形 API 请求/me/likes/{facebook_id_of_your_URL}

该调用将返回包含 URL 对象数据的数据数组,如果用户还不喜欢它,则会返回一个空数据数组。