如何让我的Facebook应用程序在安装后自动询问所需的权限

时间:2010-10-17 22:40:00

标签: php facebook

我正在使用Facebook PHP SDK(2.1.2)。我想做的就是几乎所有req_perms的Facebook应用程序都有。安装时会弹出愚蠢的“权限请求”框。

我不想要用户必须按下的按钮。我不希望出现弹出窗口。我不想使用FBML,since they're doing away with it

这是标准的Facebook权限对话框,显示我的应用程序应该在画布iframe中显示的位置。

我试过了:

<?php if(!$me): ?>
    <head>
        <meta HTTP-EQUIV="REFRESH" content="0; <?php echo $loginUrl; ?>">
    </head>
<?php die(); ?>
<?php endif; ?>

由于某种原因,显示了一个Facebook徽标链接到我想要的正确网址(不是我想要的!

<?php if($me): ?>
    window.fbAsyncInit = function() {
        FB.init({
            appId   : '<?php echo $appid; ?>',
            session : <?php echo isset($session) ? "'".json_encode($session)."'" : 'null'; ?>, // don't refetch the session when PHP already has it
            status  : true, // check login status
            cookie  : true, // enable cookies to allow the server to access the session
            xfbml   : true // parse XFBML
        });

        // Whenever the user logs in, we refresh the page
        FB.Event.subscribe('auth.login', function() {
            window.location.reload();
        });
    };

    (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    }());
 </script>
 <fb:redirect url="<?php echo $loginUrl; ?>" />
 <?php die("</html>"); ?>

这张显示空白页。

<?php if($me): ?>
    window.fbAsyncInit = function() {
        FB.init({
            appId   : '<?php echo $appid; ?>',
            session : <?php echo isset($session) ? "'".json_encode($session)."'" : 'null'; ?>, // don't refetch the session when PHP already has it
            status  : true, // check login status
            cookie  : true, // enable cookies to allow the server to access the session
            xfbml   : true // parse XFBML
        });

        // Whenever the user logs in, we refresh the page.
        FB.Event.subscribe('auth.login', function() {
            window.location.reload();
        });
    };

    (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    }());
 </script>
 <script>
     FB.login(function(response) {
         if (response.session) {
             if (response.perms.indexOf('publish_stream') != -1) {
                 //User has logged in and given us publish_stream permissions
             );
             else {
                 //User has logged in but not given us publish_stream
             }
         }
         else {
             //User is not logged in
     }, {perms:'offline_access,publish_stream'});
 </script>

这个也显示一个空白页。

我想要的并不是不受欢迎的,但Facebook文档是我遇到过的最糟糕的文档。什么都行不通。这个愚蠢的事情不应该用两天时间来解决。

以下是我最终解决的问题:

<div id="fb-root"></div>
<script>
    window.fbAsyncInit = function() {
        FB.init({
            appId   : '<?php echo $appid; ?>',
            session : <?php echo isset($session) ? "'".json_encode($session)."'" : 'null'; ?>, // don't refetch the session when PHP already has it
            status  : true, // check login status
            cookie  : true, // enable cookies to allow the server to access the session
            xfbml   : true     // parse XFBML
        });

        FB.getLoginStatus(function(response) {
            if (response.session) {
                // Logged in and connected user, someone you know.
            } else {
                // No user session available, redirect them to login.
                window.top.location = "<?php echo $loginUrl; ?>";
            }
        });

        // Whenever the user logs in, we refresh the page.
        FB.Event.subscribe('auth.login', function() {
            window.location.reload();
        });
    };

    (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
    }());
</script>

1 个答案:

答案 0 :(得分:0)

基本上,您使用PHP SDK获取登录URL,然后执行JavaScript重定向到该URL。除了重定向之外,示例http://github.com/facebook/php-sdk/blob/master/examples/example.php为您完成所有操作。在示例中,他们有一个您单击登录的链接。如果您希望它是自动的,请执行以下操作。

<script>
    FB.getLoginStatus(function(response) {
        if (response.session) {
            // Logged in and connected user, someone you know.
        } 
        else {
            // No user session available, redirect them to login.
            window.top.location = <?php echo $loginUrl; ?>
        }
    });
</script>