Redirect to Facebook page tab after authentication

I have a facebook page where I have added a tab of my application. I use to check that the user is authenticated or not using the following code:-

<?php
     $app_id = "MY APP ID";

     $canvas_page = "MY CANVAS PAGE";

     $auth_url = "https://www.facebook.com/dialog/oauth?client_id=" 
            . $app_id . "&redirect_uri=" . urlencode($canvas_page);

     $signed_request = $_REQUEST["signed_request"];

     list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

     $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

     if (empty($data["user_id"])) {
            echo("<script> top.location.href='" . $auth_url . "'</script>");
     } else {
            echo("<script> location.href='index.php'</script>");
     } 
?>

Now as CANVAS PAGE has to be the same CANVAS PAGE I have changed in the application, after logging in page redirects to https://apps.facebook.com/myappname/, but I want it to redirect to my Facebook page tab; To this one - https://www.facebook.com/myfbpage/app_82136128736123.

Answers


You are correct. The redirect_uri has to be a URL that is "owned" by the application. That means that it is listed in the applications settings.

Your canvas page will have to use a JavaScript redirect (like the one you are using). The only difference is that you need to remember that your application is running inside of an iframe. You'll have to change the location property of top as opposed to the document. Top references the top most frame on the page and that is the only way for an application to perform a proper redirect.

echo "<script language=javascript>";
echo "top.location.href ='".$page_url."';";
echo "</script>";
exit();

It's ugly and many people refuse to believe that this is the correct way to do it, but I am yet to find an alternative.


Need Your Help

How to convert JSON DateTime into readable Date & time using Knockout and custom bindings

javascript datetime knockout.js

I'm using KnockoutJS with the mapping plugin and all is working well, apart from a DateTime field which is serialized as ticks like so: /Date(x)/ where x = ticks.