0

I am struggling going round in circles getting an Facebook App to post to a Page's Wall, as that Page.

** Note: I put out a 300 point bounty to update an outdated answer of a related, well-viewed, existing question. Happy to award anyone who also answers there with the updated, working, solution

In brief:

  1. I have the App Secret and ID
  2. I have Page ID
  3. I am relying on an in-code approach to generating the token I need.
  4. It is based on a solution I saw posted on the SDK GitHub page but it does not work
  5. I am using API v 2.9
  6. I am using the latest PHP SDK

It's just been hard.

The solution I used is purely based on the one below:

<?php
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/Facebook');
require_once __DIR__ . '/Facebook/autoload.php';

use Facebook\FacebookRequest;

$app_id = {{your app-id}};
$app_secret = {{your app-secret}};

$fb = new Facebook\Facebook([
  'app_id' => $app_id,
  'app_secret' => $app_secret,
  'default_graph_version' => '{{your app-version}}', 
  ]);

$token = $fb->get(
    '/oauth/access_token?client_id=' . $app_id . '&'.
    'client_secret=' . $app_secret . '&'.
    'grant_type=client_credentials');

$get_token = $token -> getdecodedBody();
$access_token = $get_token['access_token'];

try {
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->get('/{{your facebook name}}', $access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$body = $response->getBody();

print_r($body);
?>

I am certainly not lazy. I have been stuck on this issue for weeks.

I have tried the following different solutions and have gotten stuck in either a permissions related issue that was also related to depracated functionality and API calls or getting the right token to do it.

  1. Posting to a facebook page from a website using curl (using a pure CURL approach - "The user hasn't authorized the application to perform this action")
  2. Facebook SDK returned an error: You must provide an access token. Php Facebook Api - (suggests that I support login and redirection callback - yet I do not want any user intervention everytime when posting onto the page)
  3. Graph returned an error: Invalid appsecret_proof provided in the API argument - Previous attempt was based on this example but the appsecret-proof issue
  4. Simple example to post to a Facebook fan page via PHP? - Another CURL-based approach that failed me
  5. https://stackoverflow.com/a/14212125/919426 - Best explained step by step process for permissions and tokens, but still failed

Update: Tried the following and got the error "Invalid appsecret_proof provided in the API argument"

$fb = new Facebook([
    'app_id' => 'APP_ID',
    'app_secret' => 'APP_SECRET',
    'default_graph_version' => 'v2.9',
]);

$postData = [
    'message' => "Test Message",
    'picture' => 'https://www.website.com/image.jpg'
];

try
{
    // 'LONG_LIVED_PAGE_ACCESS_TOKEN' obtained by going to https://developers.facebook.com/tools/debug/accesstoken and clicking
    // "Extend Access Token"
    $response = $fb->post('/' . 'PAGE_ID' . '/feed', $postData,"LONG_LIVED_PAGE_ACCESS_TOKEN");
}
catch (FacebookResponseException $e)
{
    var_dump("Facebook Response Exception occured: \n" . $e);
    exit;
}
catch(FacebookSDKException $e)
{
    var_dump("Generic Facebook SDK Exception occured: \n" . $e);
    exit;
}
catch(Exception $e)
{
    var_dump("Uknown Exception occured while attempting to call the Facebook SDK API: \n" . $e);
    exit;
}

There are countless more that I have tried and failed.

tinonetic
  • 7,751
  • 11
  • 54
  • 79
  • 1
    you need a page token: http://www.devils-heaven.com/facebook-access-tokens/ – andyrandy Oct 02 '17 at 07:48
  • Thanks,@luschn. Sorry for the late response The problem is that the information is outdated. ***FacebookSession*** no longer exists in the SDK – tinonetic Oct 08 '17 at 12:13
  • did you check out the part about extending the token with curl? you do not need the php sdk for that. – andyrandy Oct 09 '17 at 07:33

0 Answers0