Facebook应用程序在许可授予后重新加载

时间:2012-05-11 20:03:37

标签: php facebook facebook-graph-api facebook-apps

我有一个小Facebook应用程序,发布一些预装的图像。我改变了代码结构,所以我把所有的变量都移到了config.php中,所以它会更舒服。但之后该应用程序无法正常工作,我真的不知道为什么......在许可对话框之后,它一遍又一遍地自我演示......有人可以找到这个bug吗?

这是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>

和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;
?>

非常感谢!!

2 个答案:

答案 0 :(得分:1)

事实证明,这是服务器配置失败。

答案 1 :(得分:0)

Wrap $ user = $ facebook-&gt; getUser();在try / catch中并回显被捕获的异常。这将告诉您用户未登录的原因。


更多细节 - 在下面的评论之后。

您在Javascript中检查文档引荐来源 - 但我测试中的实际引荐来源是“http://static.ak.facebook.com/platform/page_proxy.php?v=4”,因此这将失败。这将导致循环。

要检查您是否在画布中,只需使用PHP SDK

获取signed_request
$facebook->signed_request();

如果这是空的,那么您不在画布(或页面选项卡)中,因此重定向到画布。在PHP中而不是在Javascript中执行此操作。