1
<?php

require_once 'Facebook/autoload.php';
require_once 'Facebook/Authentication/AccessToken.php';
$fb = new Facebook\Facebook([
  'app_id' => '2452542542fsrgrgrg',
  'app_secret' => '5245dffhdhr5535353535',
  'default_graph_version' => 'v2.2',
  ]);

$linkData = [
  'link' => 'http://www.example.com',
  'message' => 'User provided message',
  ];
$helper = $fb->getRedirectLoginHelper(); 
try {
  $accessToken = $helper->getAccessToken(); 
  // Returns a `Facebook\FacebookResponse` object
  $response = $fb->post('/me/feed', $linkData, $accessToken);
} 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;
}

$graphNode = $response->getGraphNode();

echo 'Posted with id: ' . $graphNode['id'];

?>

I am getting this error "Facebook SDK returned an error: You must provide an access token." I have tried to search around and I haven't got any reliable answers. Anyone who has an idea can share.

Scath
  • 3,777
  • 10
  • 29
  • 40
  • You must log the user in first to get an access token ... go check the example under https://developers.facebook.com/docs/php/howto/example_facebook_login – CBroe Aug 24 '17 at 13:30
  • @CBroe, please forgive my ignorance. If I have a PHP App that posts to a PHP page, a user has to be logged in all the time it attempts to post to the page? – tinonetic Sep 30 '17 at 21:41
  • @user919426, not if you use an extended page access token. https://developers.facebook.com/docs/facebook-login/access-tokens/expiration-and-extension – CBroe Oct 01 '17 at 04:36
  • @CBroe, using the example above, how do I then make use of the extended page access token?... will I then replace `$accessToken = $helper->getAccessToken(); ` with the token? or I do all this in-code. Yesterday, I put a 300point bounty on an existing, but outdated question. Yours, if you don't mind explaining https://stackoverflow.com/q/691425/919426 – tinonetic Oct 01 '17 at 11:07

0 Answers0