如何使用 JavaScript SDK 获取 Facebook 页面 ID



我想在自定义页面标签点击时从Facebook获取页面ID。这是我正在使用的代码。这是从Facebook获取页面ID的正确方法吗?我能够使用此代码获取访问令牌和用户 ID 值。任何帮助将不胜感激。

  window.fbAsyncInit = function () {
     FB.init({
        appId: 'xxxxxxxxx',
        status: true, 
        cookie: true, 
        xfbml: true  
    });
    FB.Event.subscribe('auth.authResponseChange', function (response) {
        // Here we specify what we do with the response any time this event occurs. 
        if (response.status === 'connected') {                                                                           
        } else if (response.status === 'not_authorized') {
            //FB.login();
        } else {
            //FB.login();
        }
    });
};
// 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));
$('.btnclick').click(function () {
    fblogin();
});
function fblogin() {           
    //FB.login();
    FB.login(function (response) {
        if (response.authResponse) {                
            var sq = FB.getAuthResponse()['signedRequest']; 
            var data = sq.split('.')[1];
            data = JSON.parse(atob(data));
            alert(data);                
        } else {
            alert('User cancelled login or did not fully authorize.');
        }
    }, { scope: 'user_location,user_hometown,user_photos,friends_photos,friends_location,friends_hometown,email,user_likes,publish_actions,manage_pages', display: 'popup' });
}

第 1 步:获得manage_pages权限

步骤 2 :

    FB.api('/me?fields=accounts', function (apiResponse) {
       //apiResponse will have page id inside apiResponse.accounts.data
    });

'/me?fields=accounts' - 帐户用于获取FB页面ID

用于获取页面详细信息表单页面 ID

FB.api('/ page id ?fields=about,albums{link},photos{link},phone,location,single_line_address', function (page) {
                //To get page details from page id
            });

相关内容

  • 没有找到相关文章

最新更新