1

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;
        }
    }
Community
  • 1
  • 1
dejjub-AIS
  • 1,501
  • 2
  • 24
  • 50

1 Answers1

0

check documentation here.

Two errors in the code

Error#1:

$scope = "scope = email,read_stream,read_friendlists,publish_stream";

Error#2:

As per the documentation here (as on now)

req_params is 'scope'

Not sure about the 'canvas' and 'fbconnect' parameters which might be potentially ignored as they are not supported any more

$redirect = $facebook->getLoginUrl(
                    array(
                        'scope' => $scope,
                        'redirect_uri' => 'https://app.facebook.com/myapp'
                        )
                    );
dejjub-AIS
  • 1,501
  • 2
  • 24
  • 50
hemc4
  • 1,623
  • 3
  • 18
  • 32
  • I am now getting "An error occured. Please try again later.". Is it related to the outdated paramters? https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/ has no "canvas" and "fbconnect" parameters – dejjub-AIS Dec 19 '12 at 09:55
  • 1
    oops looks like 'req_perms' is now just 'scope'? – dejjub-AIS Dec 19 '12 at 09:56
  • I have now figured out the exact error and edited the code in your answer. I will accept this once you accept my edits :) – dejjub-AIS Dec 19 '12 at 10:08