user3329129
user3329129

Reputation: 41

how to get facebook page id inside page fan tab using facebook javascript sdk

I'm tring to create a web app that create a fan tabs for facebook pages ...

my problem is ...

I tried this :-

FB.api(
"/{page-id}/feed",
function (response) {
  if (response && !response.error) {
    /* handle the result */
  }
}

);

How to get facebook page id inside page fan tab using facebook javascript sdk ?

this is image describing what I want to do : http://www.wtttc.com/image.php?id=25

I tried everything but no use :(

thank you

Upvotes: 4

Views: 2597

Answers (5)

Ranjan
Ranjan

Reputation: 11

You have to get signed_request POSTed to your app from server side. https://developers.facebook.com/docs/pages/tabs Go through the link get some idea about the page tab and page id.

Upvotes: 0

Yue
Yue

Reputation: 41

In order to get the page id of fan page, you need to get the signed_request from Facebook. https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin In fact, this link talk about the cavas page, but it's the same principle for the fan page.

But if you look carefully the way to get this variable, you can find that,

The signed request is sent via an HTTP POST to the URL set as your Canvas URL in the App Dashboard.

Which means, if you want to get a signed request data, you should get it through a HTTP POST. To get the page id of fan page is not possible just using javascript. Javascript is a client side language, so you can't access the POST data. What you need to do is just put your javascript code into a .jsp/.php/...or any other server-side language page. Through the server-side language page, you get the signed request and pass it to your javascript code. Here is an example in JSP:

<%String signedRequest = request.getParameter("signed_request");%><script>window.signedRequest = "<%=signedRequest%>"</script>

And in your javascript, just decode the string you got and it will contain the page id.

var signedRequest = global.signedRequest;
        var data1 = signedRequest.split('.')[1];
        data1 = JSON.parse(base64decode(data1));
        console.log(data1);

Then you can get data like this:

Object {algorithm: "HMAC-SHA256", expires: 1404925200, issued_at: 1404921078, oauth_token: "CAAUT5x1Ujq8BAPzh2ze1b4QTBkcZAtRCW6zA1xJszUasqoEPp…Fy8fAVEZAyhVaxEaq6ZBw6F4bSFI1s8xtXbBLp7oBFL4wZDZD", page: Object…}

algorithm: "HMAC-SHA256" expires: 1404925200 issued_at: 1404921078 oauth_token: "CAAUT5x1Ujq8BAPzh2ze1b4QTBkcZAtRCW6zA1xJszUasqoEPpFRfM1ln3x9pb7mLBujyug5iHUifSnyxmPHOLe030wI3H5DYXubnxbPhww9aipSnwoEr6lwctuQaGKxYvDBdZCNuFiaYIduORTWirmZC2rKL86Fy8fAVEZAyhVaxEaq6ZBw6F4bSFI1s8xtXbBLp7oBFL4wZDZD" page: Object user: Object user_id: "1519950734891144" proto: Object

In the page object, you can find page id.

Object {id: "1522695611287219", liked: true, admin: true} 

About how to decode the signed request, you can see this link https://developers.facebook.com/docs/facebook-login/using-login-with-games#checklogin It's the same way.

Hope this can help you.

Upvotes: 1

brainondev
brainondev

Reputation: 1117

This is a tested solution that uses Facebook Javascript SDK and its undocumented parseSignedRequest method.

Note: you will have to use a bit of server code and this example is for PHP, but it's the easiest I've been able to find out.

Your html page (or whatever you're serving):

<head>
.....
<script type="text/javascript">
    // set a variable with the signed_request sent to your app URL by Facebook via POST
    var signedRequest = "<?php echo $_REQUEST["signed_request"] ?>";

    FB.getLoginStatus(function (response) {
        // do not use the response.authResponse.signedRequest but the one above instead
        // and let the javascript SDK parse the good signed_request for you
        var page = this.parseSignedRequest(signedRequest).page;
        // is the current user an admin of this page? true/false
        var isAdmin = page.admin;
        // do you like this page? true/false
        var isLiked = page.liked;
        // and here is the Facebook page ID
        var pageID = page.id;
        if (response.status === 'connected') {
            // user is connected

        } else if (response.status === 'not_authorized') {
            // the user is logged in to Facebook,
            // but has not authenticated your app

        } else {
            // the user isn't logged in to Facebook.

        }

    }, true);
<script>

Hope this helps. Cheers.

Upvotes: 0

user3329129
user3329129

Reputation: 41

http://www.wtttc.com/image.php?id=25

window.top.location;

Dose not work inside the facebook page tab ... I traied that before and now ...

Any new Ideas :)

Upvotes: 0

Ar Ya
Ar Ya

Reputation: 31

You can use fql to get everything you want.
https://developers.facebook.com/docs/reference/fql/
I think this will work for you :

var currentPageID;
var url = window.top.location;
FB.api(
    {
        method: 'fql.query',
        query: "SELECT id FROM object_url WHERE url = '" + url + "'"
    },
    function(response) {
        currentPageID = response[0].id;
    }
);
alert(currentPageID);

Upvotes: 0

Related Questions