facebook: permanent Page Access Token?

后端 未结 16 2091
情书的邮戳
情书的邮戳 2020-11-22 02:39

I work on a project that has facebook pages as one of its data sources. It imports some data from it periodically with no GUI involved. Then we use a web app to show the dat

相关标签:
16条回答
  • 2020-11-22 02:53

    In addition to the recommended steps in the Vlasec answer, you can use:

    • Graph API explorer to make the queries, e.g. /{pageId}?fields=access_token&access_token=THE_ACCESS_TOKEN_PROVIDED_BY_GRAPH_EXPLORER
    • Access Token Debugger to get information about the access token.
    0 讨论(0)
  • 2020-11-22 02:53

    As all the earlier answers are old, and due to ever changing policies from facebook other mentioned answers might not work for permanent tokens.

    After lot of debugging ,I am able to get the never expires token using following steps:

    Graph API Explorer:

    1. Open graph api explorer and select the page for which you want to obtain the access token in the right-hand drop-down box, click on the Send button and copy the resulting access_token, which will be a short-lived token
    2. Copy that token and paste it in access token debugger and press debug button, in the bottom of the page click on extend token link, which will extend your token expiry to two months.
    3. Copy that extended token and paste it in the below url with your pageId, and hit in the browser url https://graph.facebook.com/{page_id}?fields=access_token&access_token={long_lived_token}
    4. U can check that token in access token debugger tool and verify Expires field , which will show never.

    Thats it

    0 讨论(0)
  • 2020-11-22 02:54

    Another PHP answer to make lives easier. Updated for Facebook Graph API 2.9 . Just fill 'er up and load.

    <?php
    $args=[
    /*-- Permanent access token generator for Facebook Graph API version 2.9 --*/
    //Instructions: Fill Input Area below and then run this php file
    /*-- INPUT AREA START --*/
        'usertoken'=>'',
        'appid'=>'',
        'appsecret'=>'',
        'pageid'=>''
    /*-- INPUT AREA END --*/
    ];
    echo 'Permanent access token is: <input type="text" value="'.generate_token($args).'"></input>';
    function generate_token($args){
        $r=json_decode(file_get_contents("https://graph.facebook.com/v2.9/oauth/access_token?grant_type=fb_exchange_token&client_id={$args['appid']}&client_secret={$args['appsecret']}&fb_exchange_token={$args['usertoken']}")); // get long-lived token
        $longtoken=$r->access_token;
        $r=json_decode(file_get_contents("https://graph.facebook.com/v2.9/me?access_token={$longtoken}")); // get user id
        $userid=$r->id;
        $r=json_decode(file_get_contents("https://graph.facebook.com/v2.9/{$userid}?fields=access_token&access_token={$longtoken}")); // get permanent token
        if($r->id==$args['pageid']) $finaltoken=$r->access_token;
        return $finaltoken;
    }
    ?>
    

    Addendum: (alternative)

    Graph 2.9 onwards , you can skip much of the hassle of getting a long access token by simply clicking Extend Access Token at the bottom of the Access Token Debugger tool, after having debugged a short access token. Armed with information about pageid and longlivedtoken, run the php below to get permanent access token.

    <?php
    $args=[
    /*-- Permanent access token generator for Facebook Graph API version 2.9 --*/
    //Instructions: Fill Input Area below and then run this php file
    /*-- INPUT AREA START --*/
        'longlivedtoken'=>'',
        'pageid'=>''
    /*-- INPUT AREA END --*/
    ];
    echo 'Permanent access token is: <input type="text" value="'.generate_token($args).'"></input>';
    function generate_token($args){
    $r=json_decode(file_get_contents("https://graph.facebook.com/v2.9/{$args['pageid']}?fields=access_token&access_token={$args['longlivedtoken']}"));
    return $r->access_token;
    }
    ?>
    

    Although the second code saves you a lot of hassle, I recommend running the first php code unless you are in a lot of hurry because it cross-checks pageid and userid. The second code will not end up working if you choose user token by mistake.

    Thanks to dw1 and Rob

    0 讨论(0)
  • 2020-11-22 02:55

    I found this answer which refers to this tool which really helped a lot.

    I hope this answer is still valid when you read this.

    0 讨论(0)
  • 2020-11-22 02:59

    Application request limit reached (#4) - FB API v2.1 and greater

    This answer led me to the "ultimate answer for us" and so it is very much related so I am appending it here. While it's related to the above it is different and it seems FB has simplified the process some.

    Our sharing counts on our site stopped worked when FB rolled over the api to v 2.1. In our case we already had a FB APP and we were NOT using the FB login. So what we needed to do was get a FB APP Token to make the new requests. This is as of Aug. 23 2016.

    1. Go to: https://developers.facebook.com/tools/explorer
    2. Select the api version and then use GET and paste the following:

      /oauth/access_token?client_id={app-id}&client_secret={app-secret}&grant_type=client_credentials
      

      You will want to go grab your app id and your app secret from your app page. Main FB Apps developer page

    3. Run the graph query and you will see:

      {
         "access_token": "app-id|app-token",
         "token_type": "bearer"
      }
      

      Where

      "app-id"
      and
      "app-token"
      will be your app id from your FB app page and the generated FB App HASH you just received.

    4. Next go test your new APP access token: FB Access Token tester

    5. You should see, by pasting the

      "app-token"
      into the token tester, a single app based token without an expiration date/time.

    In our case we are using the FB js sdk so we changed our call to be like so (please note this ONLY gets the share count and not the share and comment count combined like it used to be):

    FB.api(
        '/','GET',{
        // this is our FB app token for our FB app 
            access_token: FBAppToken,
            "id":"{$shareUrl}","fields":"id,og_object{ engagement }"
    }
    

    This is now working properly. This took a lot of searching and an official bug report with FB to confirm that we have to start making tokenized requests to the FB api. As an aside I did request that they (FB) add a clue to the Error code (#4) that mentions the tokenized request.

    I just got another report from one of our devs that our FB comment count is broken as well due to the new need for tokenized requests so I will update this accordingly.

    0 讨论(0)
  • 2020-11-22 02:59

    As of April 2020, my previously-permanent page tokens started expiring sometime between 1 and 12 hours. I started using user tokens with the manage_pages permission to achieve the previous goal (polling a Page's Events). Those tokens appear to be permanent.

    I created a python script based on info found in this post, hosted at github.com/k-funk/facebook_permanent_token, to keep track of what params are required, and which methods of obtaining a permanent token are working.

    0 讨论(0)
提交回复
热议问题