0
votes

I'm stuck trying to publish in a facebook page using the SDK in PHP 5.5. I want to make a php script to auto publish updates in a facebook page that I administer. When I make the request using the Graph API Explorer it works perfect, but I always receive the error message "(#200) The user hasn't authorized the application to perform this action" when doing from the PHP script.

These are the steps and code I'm using:

  1. Get a short-lived access token for my application using the "Get Access Token" option of the Graph API Explorer, with permissions manage_pages and publish_pages.
  2. Get a never expire access token using the getOAuth2Client of the PHP SDK.
  3. Get the page access token with the "/me/accounts" request
  4. Send the publish request using the page access token and method "PORT /pageId/feed" with params: access_token and message

After getting the page access token, if I do the publish request using the Graph API Explorer it works, but from the PHP script it fails.

Here is the complete code I'm using:

<?php
require_once __DIR__ . '/Facebook/autoload.php';

$appId = 'my_app_id';
$appSecret = 'my_app_secret';
$pageId = 'my_page_id';

$shortToken = 'token got in step 1';

$fb = new Facebook\Facebook([
  'app_id' => $appId,
  'app_secret' => $appSecret,
  'default_graph_version' => 'v2.2',
]);

$oAuth2Client = $fb->getOAuth2Client();
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($shortToken);

echo '<p>Long-live token: ' . $longLivedAccessToken . '</p>';

// Get page access_token
$request = $fb->request('GET', '/me/accounts');
$request->setParams(['access_token' => $longLivedAccessToken]);
try {
  $response = $fb->getClient()->sendRequest($request);
} 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;
}
$graphEdge = $response->getGraphEdge();
foreach ($graphEdge as $graphNode) {
  if ($graphNode['id'] == $pageId) {
    $pageAccessToken = $graphNode['access_token'];
  }
}
echo '<p>Page access token: ' . $pageAccessToken . '</p>';

$request = $fb->request('POST', '/' . $pageId . '/feed');
$request->setParams(['access_token' => $pageAccessToken, 'message' => 'Page publish test message']);

try {
  $response = $fb->getClient()->sendRequest($request);
} 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;
}
?>

An output example:

Long-live token: XXXX
Page access token: XXX
Graph returned an error: (#200) The user hasn't authorized the application to perform this action

All the tokens are valid, checked using the Facebook Access Token Debugger. The page token info contains the app id, page id and my user id, with the manage_pages, publish_pages and public_profile permissions.

My application has default settings. I'm just added a platform of type "Website" with the URL of my webpage. The app is "live" and not submitted for approval (I've just create the application to auto publish messages in a Facebook page).

I've read a lot of messages and examples here and in Internet but I do not get it to work. I hope anyone can help me.

Thank you very much!

1
So what permission did you ask for? - CBroe
Hello CBroe, I checked the manage_pages and publish_pages permissions when created the access_token. These are the permissions needed to post in a page right? - Jorge
Yes. Have you made a debug output of your page access token, and checked it using the debug tool? developers.facebook.com/tools/debug Does it mention the page as “Profile ID”, and include the publish_pages permission? - CBroe
Yes, sorry my wrong, so I can't check where is your authorization request, with publish_pages scope, I suggest you add authorization request code, let me erase my bad answer, i will post this comment in the main question. and sorry again. - Juan Ruiz de Castilla
CBroe, I check all the tokens generated with the debug tool and all are ok. Juan, no entiendo muy bien a que te refieres con "authorization request". El token inicial lo genero desde la web de developers en el Graph API Explorer con la opción de "Get Token". Te refieres a eso? Ese token es el que luego pego en la variable $shortToken. CBroe, Juan, as I mentioned in the question when I made the request with the same token, method, endpoint and parameters in the Graph API Explorer (developers.facebook.com/tools/explorer/145634995501895) it works perfect. - Jorge

1 Answers

0
votes

For what it's worth. I was having the same issue. I realised I wasn't using the page's access token, I was using the /me/ access token.

I have permissions for publish_pages and manage_pages.

For reference, I'm using the Lumen framework.

public function submitStock(Request $request){

  //first get Page token data from /me/accounts
  $pageTokenData = $this->fb->get("/me/accounts", $_SESSION['facebook_access_token'])->getDecodedBody();

  $pageToken = null;

  //check get the token for the desired app
  foreach($pageTokenData['data'] as $account){
    if($account['name'] == "AppName"){
      $pageToken = $account['access_token'];
    }
  }

  //set message text from POST
  $message = $request->input('message');

  //send post using the Pages app token, not the /me/ token
  $this->fb->post('/'.$this->page.'/feed', 
    ['message' => $message], 
    $pageToken);


  return redirect($_SERVER['HTTP_REFERER']);
}