title: How does the Facebook PHP SDK v5 getAccessToken() function work
body: <p>I have a project I'm working on where I want to use the Facebook PHP SDK v5 to post data to a Facebook page but I'm having trouble making an ajax call to my Facebook script.</p>
  
  <p>this is my <code>facebook.php</code> file:</p>
  
  <pre><code> &lt;?php
   session_start();
   echo facebook();
  
   function facebook(){
      include_once $_SERVER['DOCUMENT_ROOT']."/Facebook/autoload.php";
      $permissions = ['manage_pages', 'publish_pages'];
      $callback = "https://www.my-domain.com/my-callback-url.php?fb=redirect";
  
      $fb = new Facebook\Facebook([
        'app_id' =&gt; 'xxxxxxxxxxx',
        'app_secret' =&gt; 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
        'default_graph_version' =&gt; 'v2.10',
        ]);
  
      $helper = $fb-&gt;getRedirectLoginHelper();
  
      try {
          if (isset($_SESSION['facebook_access_token'])) {
              $accessToken = $_SESSION['facebook_access_token'];
          } else {
              $accessToken = $helper-&gt;getAccessToken();
          }
      } catch(Facebook\Exceptions\FacebookResponseException $e) {
          $res['error'] = 'Error: Graph returned a session error: ' . $e-&gt;getMessage();
          return json_encode($res);
      } catch(Facebook\Exceptions\FacebookSDKException $e) {
          $res['error'] = 'Error: Facebook SDK returned a session error: ' . $e-&gt;getMessage();
          return json_encode($res);
      }
  
      if (!isset($accessToken)) {
          $redirectUrl = $helper-&gt;getLoginUrl($callback, $permissions);
          $res['login'] =  $redirectUrl;
          return json_encode($res);
      }else{
          // rest on the script to post to a page
      }
  }
  ?&gt;
  </code></pre>
  
  <p>This is <code>my-callback-url.php</code> as well as the page that makes the ajax call to the <code>facebook.php</code> file</p>
  
  <pre><code>&lt;?php
  
  if(isset($_GET['fb'])){
      $code = $_GET['code'];
  }else{
      $code = '';
  }
  
  &lt;script type="text/javascript"&gt;
  jQuery(document).ready(function($) {
  
  var code = "&lt;?php echo $code; ?&gt;";
  
  if(code !== ''){
      postToFB(code);
  }
  
  $('#postButton').on('click',function(){
      postToFB(code);
  }
  
  function postToFB(code){
      $.ajax({
          type: "POST",
          url: 'facebook.php',
          data: {"somePostData": "someValue", "redirectCode":code},
          success: function (data) {
              var json = $.parseJSON(data);
              if(json.hasOwnProperty('login')){
                  window.location.replace(json.login);
              }
          }
      });
  }
  
  });
  &lt;/script&gt;
  
  ?&gt;
  </code></pre>
  
  <p>According to the documentstion on Facebooks website this is how Facebook says <code>getAccessToken()</code> works </p>
  
  <blockquote>
    <p>Attempts to obtain an access token from an authorization code. This method will make a request to the Graph API and return a response. If there was an error in that process a FacebookSDKException will be thrown. A FacebookSDKException will also be thrown if the CSRF validation fails.</p>
  </blockquote>
  
  <p>The problem I'm running into is when my user is on <code>my-callback-url.php</code> and clicks the <code>#postButton</code> the script makes an ajax call to <code>facebook.php</code></p>
  
  <p>If <code>facebook.php</code> can't obtain an access token it calls: </p>
  
  <pre><code> $redirect = $helper-&gt;getLoginUrl($callback, $permissions);
  </code></pre>
  
  <p>This send the <code>$redirect</code> value back to the ajax function in <code>my-callback-url.php</code> and the success method sends the user to that redirect url.</p>
  
  <p>Once the user authenticates, Facebook sends the user back to <code>my-callback-url.php</code> the full URL callback looks something like this:</p>
  
  <pre><code> https://www.my-domain.com/my-callback-url.php&amp;fb=redirect?code=xxxxxxxxxx?state=xxxxxxxxxx
  </code></pre>
  
  <p>after the callback is made, <code>my-callback-url.php</code> runs the ajax script again along with the code parameter obtained from the callback.  My question is how can I pass the code parameter obtained from the callback to the <code>getAccessToken()</code> function inside <code>facebook.php</code>?  </p>
  
  <p>It does not seem that <code>getAccessToken()</code> accepts a code parameter.  I'm wondering if I need to set the code parameter into a specific session variable in order for <code>getAccessToken()</code> to read it.  Does anyone know how to make this work?     </p>