8

I need to post messages on a Facebook page. Specifically I want to post via cron.

Here's what the API docs say:

Page Access Token – These access tokens are similar to user access tokens, except that they provide permission to APIs that read, write or modify the data belonging to a Facebook Page. To obtain a page access token you need to start by obtaining a user access token and asking for the manage_pages permission. Once you have the user access token you then get the page access token via the Graph API.

How I can obtain a user access and page access token without a page callback? Is this possible?

Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147
S. Délas
  • 91
  • 1
  • 1
  • 3

4 Answers4

14

What you need it an Extended Page Token, it is valid forever. You get one like this:

  • Authorize with the manage_pages permission (and publish_pages if you want to post as Page later), to get a User Token
  • Extend the User Token
  • Use /me/accounts?fields=access_token with the Extended User Token to get a list of all your Pages with Extended Page Tokens - or use /page-id?fields=access_token to get an Extended Page Token for a specific Page

Information about all Tokens and how to extend the User Token:

andyrandy
  • 72,880
  • 8
  • 113
  • 130
7

PHP API V5

The below code worked for me after 24 hours of head scratching .... hope this helps by the way if you need this code to work you should have completed the first two steps

  1. Should have login to facebook I used getRedirectLoginHelper
  2. Set session variable with the received user access token on the call back file $_SESSION['fb_access_token'] = (string) $accessToken;

$fbApp  = new Facebook\FacebookApp( 'xxx', 'xxx', 'v2.7' );
$fb      = new Facebook\Facebook( array(
    'app_id' => 'xxx',
    'app_secret' => 'xxx',
    'default_graph_version' => 'v2.7'
) );
$requestxx = new FacebookRequest(
    $fbApp,
    $_SESSION['fb_access_token'],//my user access token
    'GET',
    '/{page-id}?fields=access_token',
    array( 'ADMINISTER' )
);
$responset  = $fb->getClient()->sendRequest( $requestxx );
$json           = json_decode( $responset->getBody() );
$page_access    = $json->access_token;

//posting to page   
$requesty = new FacebookRequest(
    $fbApp,
    $page_access ,
    'POST',
    '/{page-id}/feed?message=Hello fans YYYYYYYYYYYYYYY'
);
$response = $fb->getClient()->sendRequest( $requesty );
var_dump( $response );
Howdy_McGee
  • 10,422
  • 29
  • 111
  • 186
f4r4
  • 563
  • 8
  • 19
  • 1
    This was the answer I was looking for! Thanks. – Evan Appleby Feb 25 '17 at 02:14
  • 2
    Please understand I checked using a php file not inside a wp-plugin, This worked for me back in 2016, and another person on 2017 feb, therefore instead of copy pasting code please check the docs and compare your code ! – f4r4 Dec 18 '17 at 08:15
5

You can get the page token this way:

$response = $fb->get('/'.$pageId.'?fields=access_token', (string)$accessToken);
$json = json_decode($response->getBody());
$page_token = $json->access_token;
$response = $fb->post('/'.$pageId.'/feed', $fbData, $page_token);
joseantgv
  • 1,943
  • 1
  • 26
  • 34
0

I've only JavaScript code, but once you have an access token, you may get the pages which can be adminstered by the given user. This will contain a page access token for each of them:

jQuery.ajax({type: "GET",
                url: "https://graph.facebook.com/v2.2/me/accounts?access_token=" + userToken,
                async: false,
                data: jsonRequest,
                dataType: "json",
                cache: false,
                success: function(data)
                {

The data given back is like:

{
  "data": [
    {
      "access_token": "CAACni8TcBB0B...cZBJfwZDZD",
      "category": "Computers/Technology",
      "name": "abc",
      "id": "...",
      "perms": [
        "ADMINISTER",
        "EDIT_PROFILE",
        "CREATE_CONTENT",
        "MODERATE_CONTENT",
        "CREATE_ADS",
        "BASIC_ADMIN"
      ]
    },
    {
      "access_token": "CAA...ZDZD",
      "category": "App Page",
      "name": "xyz",
      "id": "....",
      "perms": [
        "ADMINISTER",
        "EDIT_PROFILE",
        "CREATE_CONTENT",
        "MODERATE_CONTENT",
        "CREATE_ADS",
        "BASIC_ADMIN"
      ]
    }
  ],

access_token is your page token. You may transform the above request into PHP easily.

Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • Thanks a lot for your response. I understand how i can get the page access token. But how can i get the user token ? – S. Délas Oct 01 '15 at 19:40
  • Think i find : 1) Get an access Token from : https://developers.facebook.com/tools/explorer/ 2) Get a long live access token from https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=&client_secret=&fb_exchange_token= 3) Get a permanent access token with your precedent link – S. Délas Oct 01 '15 at 21:02