title: Facebook API not getting access token
body: <p>So, I'm using the FB login button to let users login. Upon clicking the FB login button, the user is redirected to a file called fb_redirect.php so that php can start a session.</p>
  
  <pre class="lang-js prettyprint-override"><code>window.fbAsyncInit = function() 
  {
      FB.init({
          appId: '12345', 
          status: true, 
          cookie: true, 
          xfbml: true
      });
      FB.Event.subscribe('auth.login', function(response) 
      {
          window.location = '/fb_redirect.php';
      });
  };
  </code></pre>
  
  <p>In the fb_redirect file, I have the following:</p>
  
  <pre class="lang-php prettyprint-override"><code>session_start();
  require("model/facebook_api/src/facebook.php");
  
  $facebook = new Facebook(array(
      'appId' =&gt; '12345',
      'secret' =&gt; '12345'
  ));
  
  $access_token = $facebook-&gt;getAccessToken();
  $facebook-&gt;setAccessToken($access_token);
  
  if($access_token != "") 
  {
      $user = $facebook-&gt;getUser();
  
      if($user != 0)
      {
          $user_profile = $facebook-&gt;api("/".$user);  
          $fb_id = $user_profile['id'];
          $fb_first_name = $user_profile['first_name'];
          $fb_last_name = $user_profile['last_name'];
          $fb_email = $user_profile['email'];
  
          $_SESSION['login'] = "true";
      }
  }
  
  header("Location:index.php");
  </code></pre>
  
  <p>What happen in the file is kind of confusing. It's really hit or miss. If I'm already logged into FB, and then try to login to my site with the FB login button, a screen pops up and then quickly disappears. But, it doesn't log me into my site.</p>
  
  <p>If I'm not logged into FB and try to log into my site, a screen pops up and prompts me to login to FB. When I successfully log into FB in the pop up screen, the redirect happens. But, when it does, I either get an error message telling me that I need a valid access token. Other times, it just redirect to the home page without setting the session. So, I'm guessing that the access token wasn't even set at all.</p>
  
  <p>I read somewhere that it could be that you have to manually set the access token. So, I tried:</p>
  
  <pre class="lang-php prettyprint-override"><code>$facebook-&gt;setAccessToken($access_token);
  </code></pre>
  
  <p>But, that didn't work.</p>
  
  <p>I don't know how to make sense of this. Any ideas?</p>