I got help from this post to figure out how to redirect to Facebook authorization page when someone access canvas page who has not yet authorized the app.
Now, I was expecting that after user authorizes the permissions FB will redirect to the canvas page (https://app.facebook.com/myapp). But it is redirecting to the canvas url (https://myhostingapp.com/game.php?...)
Is this expected or is there any thing we can do to control it. How do I tell the API to redirect to the canvas page after authorization?
Right now I am able to think of using the $_SERVER[HTTP_REFERER] to see if I am coming from authorization page and if yes the redirect the page to the canvas page again. But I am hoping there could be much better way of doing it
Canvas url code:
if (Config::$fbAvailalbe){ //see below how this variable is derived
echo "fb is available";
$facebook = new Facebook(array(
'appId' => Config::$appId,
'secret' => Config::$appSecret,
));
$user = $facebook->getUser();
if ($user) {
echo "app installed";
try {
// Proceed knowing you have a logged in user who's authenticated.
$profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!$user) {
echo " app not installed gonna redirect";
$scope = "scope=email,read_stream,read_friendlists,publish_stream";
$redirect = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => $scope
)
);
echo '<script>top.location="' . $redirect . '";</script>';
exit();
//header("Location: $redirect".$scope);
}
if ($profile){
$firstName = $profile['first_name'];
$sid = $profile['id'];
var_dump($profile);
}else{
echo "unable to get profile";
}
}else{
echo "fb unavailable, using dmmy";
$firstName = "Dummy Name";
$sid = Config::$testsid;
}
Snipet from Config.php
//this is to derive the $fbAvailable variable
//we get this condition satisfied when we run the page runs from fb canvas
if (isset($_SERVER['HTTP_REFERER'])){
if (substr_count($_SERVER['HTTP_REFERER'],'apps.facebook.com')){
self::$fbAvailalbe = 1;
}
}