Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get page access token with Facebook API 5.0 PHP

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?

like image 223
S. Délas Avatar asked Sep 30 '15 21:09

S. Délas


People also ask

How do I get a short live access token on Facebook?

Go to https://developers.facebook.com/tools/explorer/ and select your app from the first drop down menu, in the left. Click on the button "Get access token", and in the "Select Permissions" window, click in "Extended Permissions" and check manage_pages and publish_stream, and click in "Get Access Token" blue button.

How do you get a long live access token on Facebook?

At a high level, you obtain a long-lived token for the client by: Using a valid, long-lived access token, your server sends a request to get a code from Facebook. Facebook sends a code back to your server and you securely send this code to the client.


3 Answers

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:

  • https://developers.facebook.com/docs/facebook-login/access-tokens#extending
  • http://www.devils-heaven.com/facebook-access-tokens/
like image 193
andyrandy Avatar answered Oct 04 '22 05:10

andyrandy


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 );
like image 34
f4r4 Avatar answered Oct 04 '22 03:10

f4r4


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);
like image 31
joseantgv Avatar answered Oct 04 '22 05:10

joseantgv