title: Facebook post to fan page using php sdk5 graph api 2.4
body: <p>I'm trying to create a automatic post script to fanpage using php SDK v5 and Graph API 2.4 !</p>
  
  <p>I created two scripts: </p>
  
  <p>1 - To get the list of page access tokens using publish_actions and manage_pages permissions. (returning ok)</p>
  
  <p>2 - Offline publish to fanpage. (fail)</p>
  
  <p>1) The first script works fine: </p>
  
  <pre><code>&lt;?php
  session_start(); 
  
  require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
  
  // config APP_ID e SECRET
  define('APP_ID', 'xxxxxxxxxxxxxxx');
  define('APP_SECRET', 'xxxxxxxxxxxxxxxxxxxxxx');
  
  // instancia
  $fb = new Facebook\Facebook([
      'app_id'     =&gt; APP_ID,
      'app_secret' =&gt; APP_SECRET,
      'default_graph_version' =&gt; 'v2.4'
  ]);
  
  if(isset($_SESSION['fb_access_token'])) {
      $accessToken = $_SESSION['fb_access_token'];
  
  } else {
  
      $helper = $fb-&gt;getRedirectLoginHelper();
      try {
          $accessToken = $helper-&gt;getAccessToken();
  
          if(isset($accessToken)) {
              $oAuth2Client = $fb-&gt;getOAuth2Client();
  
              // longlived access token
              if (!$accessToken-&gt;isLongLived()) {
                  $accessToken = $oAuth2Client-&gt;getLongLivedAccessToken($accessToken); 
              }
          }
  
      } catch(Facebook\Exceptions\FacebookResponseException $e) {
          echo 'Graph returned an error: ' . $e-&gt;getMessage();
          exit;
      } catch(Facebook\Exceptions\FacebookSDKException $e) {
          echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
          exit;
      }
  }
  
  if(isset($accessToken)) {
      // Logged in!
      $_SESSION['fb_access_token'] = (string) $accessToken;
  
      try {
          $response = $fb-&gt;get('/me/accounts', $accessToken);
  
          echo '&lt;pre&gt;';
          print_r($response-&gt;getDecodedBody());
          echo '&lt;/pre&gt;';
  
      } catch(Facebook\Exceptions\FacebookResponseException $e) {
          echo 'Graph returned an error: ' . $e-&gt;getMessage();
          exit;
      } catch(Facebook\Exceptions\FacebookSDKException $e) {
          echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
          exit;
      } 
  } else {
      $helper = $fb-&gt;getRedirectLoginHelper();
  
      $redirect_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
      $loginUrl = $helper-&gt;getLoginUrl($redirect_url, [ 'manage_pages', 'publish_actions' ]);
      echo '&lt;a href="' . $loginUrl . '"&gt;Log in with Facebook!&lt;/a&gt;';
  }
  </code></pre>
  
  <p>2) The second script fails: (the script should work even if I am disconnected from facebook):</p>
  
  <pre><code>&lt;?php
  session_start(); 
  
  // autoloader 
  require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
  
  define('APP_ID', 'xxxxxxxxxxxxxxx');
  define('APP_SECRET', 'xxxxxxxxxxxxxxxxxxxxxx');
  $page_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  
  // instance
  $fb = new Facebook\Facebook([
      'app_id'     =&gt; APP_ID,
      'app_secret' =&gt; APP_SECRET,
      'default_graph_version' =&gt; 'v2.4',
  ]);
  
  $linkData = [
    'link' =&gt; 'http://www.example.com',
    'message' =&gt; 'User provided message',
  ];
  
  try {
      $response = $fb-&gt;post('/me/feed', $linkData, $page_access_token);
  
  } catch(Facebook\Exceptions\FacebookResponseException $e) {
      echo 'Graph returned an error: ' . $e-&gt;getMessage();
      exit;
  } catch(Facebook\Exceptions\FacebookSDKException $e) {
      echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
      exit;
  }
  </code></pre>
  
  <p>I`m getting the error:</p>
  
  <blockquote>
    <p>Graph returned an error: Error validating access token: Session does not match current stored session. This may be because the user changed the password since the time the session was created or Facebook has changed the session for security reasons.</p>
  </blockquote>
  
  <p>In old graph api versions with php sdk v3, it works in a similar way...</p>
  
  <p>What's wrong ?</p>