2
votes

Every time I run this script it gives me a different access token. I thought the access token was unique to a user.. Also why is this script so unreliable, fails half the time.

My Facebook Access Token Page

Here is the code:

<?php
require 'lib/facebook.php';
require_once('C:\inetpub\storeboard.com\linkedin\extract_data.php');

$facebook = new Facebook(array(
  'appId'  => FACEBOOK_APPID,
  'secret' => FACEBOOK_APP_SECRET
));

$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

$access_token = $facebook->getAccessToken();
if (isset($access_token)) {
   echo $access_token;
    //header( 'Location: http://www.'.APP_ROOT_DOMAIN.'/facebook/save_token.asp?nToken='.$access_token);
}

?>

Have I cut out code that is required from the original: http://developers.facebook.com/blog/post/534/ or am I missing something.

require_once('C:\inetpub\storeboard.com\linkedin\extract_data.php');

This line of code just extracts the APPID and APPSECRET from the database.

I am a complete beginner when it comes to PHP, so any help would be greatly appreciated.

Many Thanks!

1
user access tokens have an expiry of about 2 hours, calling will not always get you a new token, but if it is expired you will get a new token. - ShawnDaGeek

1 Answers

0
votes

You don't need to play with the token to get simple authentication:

After you get facebook api instance:

$facebook = new Facebook(array(
  'appId'  => FACEBOOK_APPID,
  'secret' => FACEBOOK_APP_SECRET
));

If you are using me API - get the user profile:

$user_profile = $facebook->api('/me');

then obtain email from it:

$email = $user_profile['email'];

At this point you check if the email is valid in your database - if yes start the user session and you are good to go.