9

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?

4
  • Are you trying to do it from cron, or PHP? Sep 30, 2015 at 22:00
  • @NateBarbettini even with cron it can be PHP, this is not excluding each other ... ? Sep 30, 2015 at 22:02
  • @AxelAmthor Fair enough, I forgot about that. Sep 30, 2015 at 22:04
  • Exactly, it's not a Linux CRON but a planned task in a PHP program. But like NateBarbettini say we can do a CRON with a PHP script so it is the same thing in my case.
    – S. Délas
    Oct 1, 2015 at 19:58

4 Answers 4

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:

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 );
2
  • 1
    This was the answer I was looking for! Thanks. Feb 25, 2017 at 2: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, 2017 at 8: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);
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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.