1

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' });
}
sainath sagar
  • 499
  • 2
  • 10
  • 28
  • This link might help you: https://developers.facebook.com/docs/reference/login/signed-request/ - you can find the page id in signed request. – Jurik Jan 06 '14 at 09:52
  • I know that it returns few parameters, but all i am getting from signed_request is some code, issued_at and userid – sainath sagar Jan 06 '14 at 10:17
  • Yes and in this 'some code' are details to the page in json format: `containing the page id string, the liked boolean (set to true if the user has liked the page, false if not) and the admin boolean` – Jurik Jan 06 '14 at 10:33
  • Actually it wasn't any json format string. Just it is line of code thats it. You Mean that alone can fetch me the page id?? – sainath sagar Jan 06 '14 at 11:17
  • Please post the line of code you get with your signed_request. – Jurik Jan 06 '14 at 11:25
  • Here is the code, Wu3S-Rl2OMV_aE0wUiWGflT0P3Z1yeDAavo0YRtxJELexqm7zvMMYb1oo-tajGUMiSq3kXMRTTSoTRNk5uISq03cMvsr8v-8NFq_iBkgzArr3cDcwwKLRvbSBlw4Owm1Dxyq3iup0jkZCDdyyAnAu1kzopeBmIDyerfUpLH9k7d948bio1WOlf6mYC5uV-bmy0T9NYpdj3n4sytmqSc3jb5OVabsGCpi_1Cid\", issued_at:1389007903, user_id:\"xxxxxxxxxx\"})")) – sainath sagar Jan 06 '14 at 11:32
  • var sq = FB.getAuthResponse()['signedRequest']; var data = sq.split('.')[1]; var res = JSON.parse(atob(data)); var pg = res.toSource(); – sainath sagar Jan 06 '14 at 11:34
  • Almost correct - check out this: http://stackoverflow.com/questions/5093398/how-to-check-if-a-user-likes-my-facebook-page-or-url-using-facebooks-api there's a script you need to use to get the data from signed request – Jurik Jan 06 '14 at 11:38
  • I think those codes emphasis on php sdk but i want it through javascript code. – sainath sagar Jan 06 '14 at 11:41
  • hey @sainathsagar have you managed how to get the page id with javascript? i have the same problem. – Marco Caltagirone Oct 27 '15 at 11:42

1 Answers1

1

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
            });
sagivasan
  • 97
  • 7