简体   繁体   中英

Facebook redirects to canvas url instead of the app on facebook

I wrote a facebook app that works as a tab on a page.

Here is my config.php file which contains the configuration and redirects to the authentication page if the user has not yet approved the app:

<?php
require_once("database.php");
require_once("functions.php");
require_once("facebook.php");

$config = array
(
    'database' => array
    (
        'host' => 'localhost',
        'name' => '***',
        'username' => '***',
        'password' => '***'
    )
);

$db = new Database($config['database']['host'], $config['database']['name'], $config['database']['username'], $config['database']['password']);
$db->connect();

$facebook = new Facebook(array(
    'appId' => '***',
    'secret' => '***',
    'cookie' => true
));

$user = $facebook->getUser();

if ($user)
{
    try
    {
        $me = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }
}

if ($user)
    $logoutUrl = $facebook->getLogoutUrl();
else
{
    $loginUrl = $facebook->getLoginUrl(array(
        'scope' => 'email,user_about_me,user_likes,user_status,read_stream,offline_access,publish_stream,publish_actions'
    ));
}

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

Here is my main file:

    <?php
    require_once("includes/config.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="he" lang="he" dir="rtl">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=windows-1255" />
        <title>
            שער 7 - הלב הצהוב | מכבי תל אביב
        </title>
    </head>
    <body>
        <?php
            $clLike = $facebook->api('/me/likes/204623816223540');
            if (!empty($clLike['data']))
                echo "I'm a fan!";
            else
                echo "I'm not a fan :(";
        ?>
    </body>
</html>

As I mentioned before, this app works as a tab on a facebook page and not as a stand-along facebook app, therefore there is no canvas url. The page tab url is: http://www.gate7.co.il/fbapps/predictor/ .

When a user enters the tab, he gets redirected to the authentication page (from the config.php), but when the authentication process is done, the user gets redirected to the page tab url (http://www.gate7.co.il/fbapps/predictor/) instead of the tab inside the page.

I tried to add these lines to the index file:

        <script type="text/javascript">
        if (top.location.href != "http://www.facebook.com/gate7yellowheart?sk=app_196175180456372")
            window.location = "http://www.facebook.com/gate7yellowheart?sk=app_196175180456372";
    </script>

It worked well on firefox, and the user got redirected from the page tab url to the actual tab in the page, but in chrome it just caused mess and showed facebook inside the tab.

What's the solution for this?

In your getLoginUrl() call, set the redirect_uri param to your tab page, like this:

$loginUrl = $facebook->getLoginUrl(array(
    'scope' => 'email,user_about_me,user_likes,user_status,read_stream,offline_access,publish_stream,publish_actions',
    'redirect_uri' => 'http://www.facebook.com/gate7yellowheart?sk=app_196175180456372'
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM