title: How to get access token facebook api correct way?
body: <p>Just playing around with facebook api but couldn't get the basic setup.Downloaded the sdk from facebook<a href="https://github.com/facebook/facebook-php-sdk-v4" rel="nofollow">SDK</a>. I cant seem to get the access token working
      
  
  <pre><code>$fb = new Facebook\Facebook([
    'app_id' =&gt; 'keyxx',
    'app_secret' =&gt; 'key',
    'default_graph_version' =&gt; 'v2.4',
    ]);
  $helper = $fb-&gt;getRedirectLoginHelper();
  
    $accessToken = $helper-&gt;getAccessToken();
  
  try {
    // Returns a `Facebook\FacebookResponse` object
    $response = $fb-&gt;get('/me?fields=id,name',$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;
  }
  
  $user = $response-&gt;getGraphUser();
  
  echo 'Name: ' . $user['name'];
  
  ?&gt;
  </code></pre>
  
  <p>Forgot to mention that my app is a canvas
  This is another try by redirecting which does not work. All i see is a blank canvas</p>
  
  <pre><code>&lt;?php
  
  require_once 'facebook-php-sdk-v4/src/Facebook/autoload.php';
  session_start()
  echo 'hello';
  
  
  
  $fb = new Facebook\Facebook([
    'app_id' =&gt; 'x',
    'app_secret' =&gt; 'y',
    'default_graph_version' =&gt; 'v2.4',
    'default_access_token' =&gt; isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'x|y'
  
    ]);
  
  
  try {
    $response = $fb-&gt;get('/me?fields=id,name');
    $user = $response-&gt;getGraphUser();
    echo 'Name: ' . $user['name'];
    exit; //redirect, or do whatever you want
  } catch(Facebook\Exceptions\FacebookResponseException $e) {
    //echo 'Graph returned an error: ' . $e-&gt;getMessage();
  } catch(Facebook\Exceptions\FacebookSDKException $e) {
    //echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
  }
  
  $helper = $fb-&gt;getRedirectLoginHelper();
  $permissions = ['email', 'user_likes'];
  $loginUrl = $helper-&gt;getLoginUrl('http://www.unsquaringthewheel.com/newTemp/login-callback.php', $permissions);
  echo '&lt;a href="' . $loginUrl . '"&gt;Log in with Facebook!&lt;/a&gt;';
  
  ?&gt;
  </code></pre>
  
  <p>This is the callback login page</p>
  
  <pre><code>&lt;?php
  require_once 'facebook-php-sdk-v4/src/Facebook/autoload.php';
  
  session_start();
  
  $fb = new Facebook\Facebook([
    'app_id' =&gt; 'x',
    'app_secret' =&gt; 'y',
    'default_graph_version' =&gt; 'v2.4',
    'default_access_token' =&gt; 'x|y'
  ]);
  
  $helper = $fb-&gt;getRedirectLoginHelper();
  
  try {
    $accessToken = $helper-&gt;getAccessToken();
  } catch(Facebook\Exceptions\FacebookResponseException $e) {
    // When Graph returns an error
    //echo 'Graph returned an error: ' . $e-&gt;getMessage();
  } catch(Facebook\Exceptions\FacebookSDKException $e) {
    // When validation fails or other local issues
    //echo 'Facebook SDK returned an error: ' . $e-&gt;getMessage();
  }
  
  if (isset($accessToken)) {
    // Logged in!
    $_SESSION['facebook_access_token'] = (string) $accessToken;
  } elseif ($helper-&gt;getError()) {
    // The user denied the request
  }
  header('Location: index.php');
  </code></pre>