Charles Kirk
Charles Kirk

Reputation: 167

Facebook SDK 3.1 getUser most basic code not working

I had some really complicated code, and now I've made it ridiculously simple, and it doesn't work.

Currently it simply takes me back to the page with the login URL echoed out.

Code is here:

<?php
require 'facebook.php';

// Create our application instance
// (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'sd',
  'secret' => 'sda',
));

// Get User ID
$user = $facebook->getUser();


if($user) {
    echo $user;
} else {
    $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>'http://www.facebook.com/pages/CharlesTestPage/225802194155435?sk=app_252946408094785','scope'=>'email'));
  echo $loginUrl;
}
exit;
?>

Please. I have spent a whole paid work day on this now, and am at the point of crying, not only for myself but for my boss.

Cry.

Edit: OK, the weird thing is, if I have the redirect_uri set to the facebook tab, if it's not authenticated itself, then it constantly redirects in an infinite loop. However, if I remove the redire

Upvotes: 2

Views: 1879

Answers (2)

Von Talavang
Von Talavang

Reputation: 9

Check for

if(isset($_GET['code'])){

    $user = $facebook->getUser();
    $access_token = $facebook->getAccessToken();

}

Facebook return userID only after the login and that code.

Upvotes: -1

DevGW
DevGW

Reputation: 703

This is what's working for me using PHP SDK 3.1.1. Try it and let us know if this works:

include('facebook.php');
session_start();

//facebook application
$config['appid' ] = "YOUR_APP_ID";
$config['secret'] = "YOUR_APP_SECRET";
$config['baseurl'] = "http://example.com/facebookappdirectory";
$config['appbaseurl'] = "http://apps.facebook.com/your-app-name";


// Create our Application instance.
$facebook = new Facebook(array(
  'appId'  => $config['appid'],
  'secret' => $config['secret'],
  'cookie' => true,
));

$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
        array(
            'scope' => 'email'
        )
);

if ($user) {
  try {
    //get user basic description
    $userInfo = $facebook->api("/$user");
    $fb_access_token = $facebook->getAccessToken();
  } catch (FacebookApiException $e) {
    //you should use error_log($e); instead of printing the info on browser
    error_log('APP ERROR: '.$e); 
    $user = null;
  }
}

if (!$user) {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    exit;
}

if (isset($_GET['code'])){
    header("Location: " . $config['appbaseurl']);
    exit;
}

Upvotes: 2

Related Questions