title: Storing Access Token
body: <p>I have been trying to solve this problem for three days now, it's really simple for those who know the open graph api well. I'm new to Facebook integration but have some PHP experience.</p>
  
  <p>Basically all I'm trying to do is retrieve the following information from users and store it in a database.</p>
  
  <p>Facebook User ID:
  Name
  Gender
  Email</p>
  
  <p>I have done the user ID, name and gender by using:</p>
  
  <pre><code>$contents = file_get_contents ('https://graph.facebook.com/'.$user);
  $json=json_decode($contents,true);
  $userid = $json['id'];
  $username = $json['name'];
  $usergender = $json['gender'];
  $useremail = $json['email'];
  </code></pre>
  
  <p>This works and I understand I need to ask for permissions to access the email which I have done using this code:</p>
  
  <pre><code>$app_id = "211665122244023";
  
       $canvas_page = "http://apps.facebook.com/midcitymafia/";
  
       $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
              . $app_id . "&amp;redirect_uri=" . urlencode($canvas_page) . "&amp;scope=email,publish_actions";
  
       $signed_request = $_REQUEST["signed_request"];
  
       list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
  
       $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
  </code></pre>
  
  <p>But how do I get the access token and then use it to retrieve email from graph?</p>
  
  ##UPDATE, THIS IS MY CURRENT CODE, STILL CAN'T SEEM TO GET IT TO WORK...
  
  <pre><code>require 'src/facebook.php';
  $app_id = "211665122244023";
  
       $canvas_page = "http://apps.facebook.com/midcitymafia/";
  
       $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
              . $app_id . "&amp;redirect_uri=" . urlencode($canvas_page) . "&amp;scope=email,publish_actions";
  
       $signed_request = $_REQUEST["signed_request"];
  
       list($encoded_sig, $payload) = explode('.', $signed_request, 2); 
  
       $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
  
  
       if (empty($data["user_id"])) {
              echo("&lt;script&gt; top.location.href='" . $auth_url . "'&lt;/script&gt;");
       } else {
           $graph = json_decode(file_get_contents("https://graph.facebook.com/".$user_id . "/?accesstoken=" .$data['access_token']));
       }
  
  $userid = $graph-&gt;id;
  $username = $graph-&gt;name;
  $usergender = $graph-&gt;gender;
  $useremail = $graph-&gt;email;
  
  ?&gt;
  
  &lt;br&gt;
  
  &lt;?php echo 'ID: ' . $userid; ?&gt;
  &lt;br&gt;
  &lt;?php echo 'Name: ' . $username; ?&gt;
  &lt;br&gt;
  &lt;?php echo 'Gender: ' . $usergender; ?&gt;
  &lt;br&gt;
  &lt;?php echo 'Email: ' . $useremail; ?&gt;
  </code></pre>