Search code examples
facebookoauthfacebook-php-sdkauthorizeauthentication

php-sdk 3.1.1 can't get $facebook->api('/me') for test user and no authorization request displays


When I use the following code, $user_profile = $facebook->api('/me'); will show the info for me, the admin user, but when I switch to a test user the permissions dialog never displays for the test user to add the app and $user_profile doesn't get defined.

I can't seem to find one example of code that seems to do everything properly in terms of making sure the app is authorized and the user is authenticated. I see from the old FB developer forums that a lot of people seem to be having the same issue with the newer procedures.

Here's the code:

<?php
$appId  = "myid";
$secret = "mysecret";
$canvasurl = "http://www.example.com/myappname/";
$canvas = "http://apps.facebook.com/myappname/";
$scope  = "user_website,email,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,manage_pages,offline_access";
require_once "facebook.php";
$facebook = new facebook(array(
'appId' => $appId,
'secret' => $secret
)
);
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e) {
$user = null;
}

if (!$user) {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $scope,
'redirect_uri' => $canvas
)
);

echo <<<LU
<script type="text/javascript">
top.location.href = $loginUrl;
</script>
LU;
}
}
print_r($user_profile);
?>

Thanks.


Solution

  • I will post my code

    <?php
        $fbconfig['appid' ] = "";
        $fbconfig['secret'] = "";
    
        $fbconfig['baseUrl']    =   "";
        $fbconfig['appBaseUrl'] =   "";
    
        $user            =   null;
    
        try{
          include_once "src/facebook.php";
        }
        catch(Exception $o){
          echo '<pre>';
          print_r($o);
          echo '</pre>';
        }
    
    
        $facebook = new Facebook(array(
           'appId'  => $fbconfig['appid'],
           'secret' => $fbconfig['secret'],
           'cookie' => true,
        ));
    
        //Facebook Authentication part
    
        $user       = $facebook->getUser();
    
        $loginUrl   = $facebook->getLoginUrl(
            array(
                'canvas'    => 1,
                'fbconnect' => 0,
                'scope'         => 'user_status,publish_stream,user_photos'
            )
        );
    
        if ($user) {
          try {
    
            $user_profile = $facebook->api('/me');
    
          } catch (FacebookApiException $e) {
            $user = null;
          }
        }
    
        if (!$user) {
             echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
             exit;
        }
    ?>