'Facebook Page Access Token

i want to bind the rating information from Facebook Page into my website.

First i get the Information from the Graph API Explorer, and that works well.

Know i have tried to programming this with javascript and i have problems with the page access token.

      FB.api('/{page-id}/ratings', function(response) {
          console.log(response);

      });

The Stacktrace:

enter image description here

How can i include/programming the Page Access Token into the function?



Solution 1:[1]

You need to use a Page Token, and you need to use it on your server - do not use the JavaScript SDK for this. Using a Page Token on the client would be a bad idea, you should never expose a Token on the client - especially not an Extended Page Token, which is what you would want to use in that case).

How to generate an Extended Page token is explained in the docs, here are some links:

Here“s a general tutorial:

  • Get a User Token with the manage_pages permission
  • Extend the User Token
  • Get an Extended Page Token with /page-id?fields=access_token

You should also consider caching the results in your database instead of reading from the Facebook API whenever a new user hits your page.

Solution 2:[2]

To access page specific data, first you need to obtain a page access token for the specific page and then call the api along with that token.

//Load FB js SDK here

function getpages() {
    FB.api('/me/accounts', function(response) {
        console.log('page details');
        console.log(response);
        $.each(response.data,function(index, item){
            $("#fbpgs").append("<div class='row'>"+item.name+"<button onclick='getads("+item.id+",\""+item.access_token+"\")' >Show details</button></div>");
        });
    });
}

function get_page_data(pid,token) {
    $("#lead_forms").html("");
    $(".dv").removeClass("seldiv");
    $(dv).parent().parent().addClass("seldiv");
    FB.api("/"+pid+"/leadgen_forms",{access_token : token}, function(response) {
        console.log('page details');
        console.log(response);
    });
}

<button onclick='getpages()'>load pages</a>
<div class='fbpgs'></div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2