VORiAND
VORiAND

Reputation: 175

Facebook app reloads himself after permission grant

I have a little Facebook app, that posts some preloaded images. I changed the code structure, so i moved all the variables into config.php, so it would be more comfortable. But after that the app doesn't work and i really don't know why... After the permission dialog it realoads himself over and over... Can someone fing the bug?

This is the index.php:

<?php

require 'facebook.php';
require 'config.php';

$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
'baseUrl' => $baseUrl,
'appBaseUrl' => $appBaseUrl,
'fileUpload' => 'true',
'cookie' => 'true'
));

if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) {
    $baseUrl = str_replace('http','https',$baseUrl);
    $fbPageURL = str_replace('http','https',$fbPageURL);
    $fbPageAppURL = str_replace('http','https',$fbPageAppURL);
}else{
    // not https
}

    $signed_request = $facebook->getSignedRequest();
    $page_id = $signed_request["page"]["id"];
    $like_status = $signed_request["page"]["liked"];

        if(!$like_status){
            header("Location: notfan.php");
            exit;
        }

$user = $facebook->getUser();

$params = array(
  scope => 'publish_stream,user_photos',
  redirect_uri => $fbPageAppURL
    );

$loginUrl = $facebook->getLoginUrl($params);
$app_data = $signed_request["app_data"];

?>
                    <!doctype html>
                    <html xmlns:fb="http://www.facebook.com/2008/fbml">
                    <head>
                    <title><?php echo $appName; ?></title>
                    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                    <style>
                        a:link    {color:#f1effc;}
                        a:visited {color:#f1effc;}
                        a:hover   {color:#19378d;}
                        a:active  {color:#19378d;}
                        a:link    {text-decoration: none}
                    </style>
                    </head>
                    <body>
                    <script type="text/javascript">
                        function NotInFacebookFrame() {
                            return top === self;
                         }
                        function ReferrerIsFacebookApp() {
                            if(document.referrer) {
                                return document.referrer.indexOf("apps.facebook.com") != -1;
                        }
                         return false;
                         }
                        if (NotInFacebookFrame() || ReferrerIsFacebookApp()) {
                            top.location.replace("<?= $fbPageAppURL ?>");
                        }
                    </script>

<?php if($user){

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

                $scope = 'publish_stream,user_photos'; 
                $scope_params = explode(',',$scope);

                $permissions = $facebook->api("/me/permissions"); 

                    if( array_key_exists('publish_stream', $permissions['data'][0]) &&  array_key_exists('user_photos', $permissions['data'][0])) {

                    // the main app logic here

                    } else {
                        $user = null;
                    }


    } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
    }

    if ($user) {
    // logged in user
    } else {
        // not logged in or not authenticated
        echo '<script type="text/javascript">top.location.href = "'.$loginUrl .'";</script>';
    }

?>  
</body>
</html>

and the config.php:

<?php
$appName = 'the name';
$appId = '123456789';
$appSecret = '1324v3434v34v';
$baseUrl = 'http://www.domain.com/app-name/';
$appBaseUrl = 'http://apps.facebook.com/zzzzzzz/';
$fbPageURL = 'http://www.facebook.com/xxxxxxx';
$fbPageAppURL = $fbPageURL.'?sk=app_'.$appId;
?>

Thank you very much!!

Upvotes: 0

Views: 561

Answers (2)

VORiAND
VORiAND

Reputation: 175

As it turned out, it was a server configuration failure.

Upvotes: 1

Robbie
Robbie

Reputation: 17710

Wrap $user = $facebook->getUser(); in a try/catch and echo the exception that gets caught. That will tell you why the user is not getting logged on.


More details - after the comments below.

You check document referrer in your Javascript - but the actual referrer in my tests was "http://static.ak.facebook.com/platform/page_proxy.php?v=4" so this would fail that condition. This will causes a loop.

To check if you're in a canvas, just get the signed_request using the PHP SDK

$facebook->signed_request();

If this is empty then you're not in a canvas (or a page tab) so redirect to the canvas. Do that in PHP and not in Javascript.

Upvotes: 0

Related Questions