how to get facebook page id using javascript sdk?

1.9k views Asked by At

I want to get page id from Facebook on custom page tab click. Here is the code, i am working with. Is this right way to get page id from the Facebook. I am able to get access token and user id values using this code. Any help is greatly appreciated.

  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

There are 1 answers

0
sagivasan On

Step 1 : get manage_pages permission

step 2 :

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

'/me?fields=accounts' - account is used to get fb pageid

For Getting page Detail form page 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
            });