facebook: permanent Page Access Token?

后端 未结 16 2156
情书的邮戳
情书的邮戳 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 03:02

    Following the instructions laid out in Facebook's extending page tokens documentation I was able to get a page access token that does not expire.

    I suggest using the Graph API Explorer for all of these steps except where otherwise stated.

    0. Create Facebook App

    If you already have an app, skip to step 1.

    1. Go to My Apps.
    2. Click "+ Add a New App".
    3. Setup a website app.

    You don't need to change its permissions or anything. You just need an app that wont go away before you're done with your access token.

    1. Get User Short-Lived Access Token

    1. Go to the Graph API Explorer.
    2. Select the application you want to get the access token for (in the "Application" drop-down menu, not the "My Apps" menu).
    3. Click "Get Token" > "Get User Access Token".
    4. In the pop-up, under the "Extended Permissions" tab, check "manage_pages".
    5. Click "Get Access Token".
    6. Grant access from a Facebook account that has access to manage the target page. Note that if this user loses access the final, never-expiring access token will likely stop working.

    The token that appears in the "Access Token" field is your short-lived access token.

    2. Generate Long-Lived Access Token

    Following these instructions from the Facebook docs, make a GET request to

    https://graph.facebook.com/v2.10/oauth/access_token?grant_type=fb_exchange_token&client_id={app_id}&client_secret={app_secret}&fb_exchange_token={short_lived_token}

    entering in your app's ID and secret and the short-lived token generated in the previous step.

    You cannot use the Graph API Explorer. For some reason it gets stuck on this request. I think it's because the response isn't JSON, but a query string. Since it's a GET request, you can just go to the URL in your browser.

    The response should look like this:

    {"access_token":"ABC123","token_type":"bearer","expires_in":5183791}

    "ABC123" will be your long-lived access token. You can put it into the Access Token Debugger to verify. Under "Expires" it should have something like "2 months".

    3. Get User ID

    Using the long-lived access token, make a GET request to

    https://graph.facebook.com/v2.10/me?access_token={long_lived_access_token}

    The id field is your account ID. You'll need it for the next step.

    4. Get Permanent Page Access Token

    Make a GET request to

    https://graph.facebook.com/v2.10/{account_id}/accounts?access_token={long_lived_access_token}

    The JSON response should have a data field under which is an array of items the user has access to. Find the item for the page you want the permanent access token from. The access_token field should have your permanent access token. Copy it and test it in the Access Token Debugger. Under "Expires" it should say "Never".

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

    In addition to mentioned methods it is worth mentioning that for server-to-server applications, you can also use this form of permanent access token: app_id|app_secret This type of access token is called App Token. It can generally be used to call Graph API and query for public nodes within your application back-end. It is mentioned here: https://developers.facebook.com/docs/facebook-login/access-tokens

    0 讨论(0)
  • 2020-11-22 03:04

    If you are requesting only page data, then you can use a page access token. You will only have to authorize the user once to get the user access token; extend it to two months validity then request the token for the page. This is all explained in Scenario 5. Note, that the acquired page access token is only valid for as long as the user access token is valid.

    0 讨论(0)
  • 2020-11-22 03:08

    Many of these examples do not work, not sure if it's because of 2.9v coming out but I was banging my head. Anyways I took @dw1 version and modified it a little with the help of @KFunk video and got this working for me for 2.9. Hope this helps.

    $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/{$args['pageid']}?fields=access_token&access_token={$longtoken}")); // get user id
        $finaltoken=$r->access_token;
        return $finaltoken;
    }
    
    0 讨论(0)
  • 2020-11-22 03:12

    Here's my solution using only Graph API Explorer & Access Token Debugger:

    1. Graph API Explorer:
      • Select your App from the top right dropdown menu
      • Select "Get User Access Token" from dropdown (right of access token field) and select needed permissions
      • Copy user access token
    2. Access Token Debugger:
      • Paste copied token and press "Debug"
      • Press "Extend Access Token" and copy the generated long-lived user access token
    3. Graph API Explorer:
      • Paste copied token into the "Access Token" field
      • Make a GET request with "PAGE_ID?fields=access_token"
      • Find the permanent page access token in the response (node "access_token")
    4. (Optional) Access Token Debugger:
      • Paste the permanent token and press "Debug"
      • "Expires" should be "Never"

    (Tested with API Version 2.9-2.11, 3.0-3.1)

    0 讨论(0)
  • 2020-11-22 03:12

    I made a PHP script to make it easier. Create an app. In the Graph API Explorer select your App and get a user token with manage_pages and publish_pages permission. Find your page's ID at the bottom of its About page. Fill in the config vars and run the script.

    <?php
    $args=[
        'usertoken'=>'',
        'appid'=>'',
        'appsecret'=>'',
        'pageid'=>''
    ];
    
    echo generate_token($args);
    
    function generate_token($args){
        $r=json_decode(file_get_contents("https://graph.facebook.com/v2.8/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.8/me?access_token={$longtoken}")); // get user id
        $userid=$r->id;
        $r=json_decode(file_get_contents("https://graph.facebook.com/v2.8/{$userid}/accounts?access_token={$longtoken}")); // get permanent token
        foreach($r->data as $d) if($d->id==$args['pageid']) return $d->access_token;
    }
    
    0 讨论(0)
提交回复
热议问题