Facebook app does not redirect mobile site url

user411103 picture user411103 · Oct 31, 2012 · Viewed 7.7k times · Source

On my app settings I have as mobile URL this one:

https://example.herokuapp.com/mobile.php#&ui-state=dialog

However, when I try to access the app from Facebook using a smartphone, Facebook redirects to an OAuth URL for a few milliseconds, and then it redirects to:

https://example.herokuapp.com/?state=63903485c518f2ae5deca667b9a............#_=_

As the /mobile.php is lost, the desktop version is loaded instead.

How can I fix this? Any advice is very much appreciated.

Thank you very much

Edit: piece of security code

try {
    $facebook = new Facebook(array(
            'appId'  => AppInfo::appID(),
            'secret' => AppInfo::appSecret(),
    ));
    $access_token=$_GET['access_token'];
    Log::debug("BaseControl:access token: ". $access_token);
    if(isset($access_token)){
        $facebook->setAccessToken($access_token);
    }
    $user_id = $facebook->getUser();
} catch (Exception $e) {
    exit("Error getting facebook data");
}

if ($user_id) {
    try {
        $basic = $facebook->api('/me');//se necesita access token, si no se tiene falla
    } catch (FacebookApiException $e) {
        if (!$facebook->getUser()) {
            exit("Invalid access token");
        }
    }
    if($basic==null){
        exit("Application not installed");
    }
    $user=UsersLogic::getUser($user_id);
    if($user==null){
        exit("User not registered in database");
    }

}
else{
    exit("No user logged");
}

Answer

Jesse Chen picture Jesse Chen · Nov 3, 2012

Arturo, I would double check that your dashboard settings are correct. Make sure the "Website with Facebook login" section has the correct URL, which in your case, is the mobile one. The tooltip when you hover over the [?] says "URL for your website. For security reasons, we will only redirect to this URL". Thus, in order to have the correct redirect URL you need to make sure that the URL in that field is the URL you wish the user to redirect to.

As it says, Facebook will only redirect to that URL for security purposes, so if you have a desktop and mobile version of your app, then you need to have one endpoint to handle the redirect to send the user to the correct page. For your case, it seems like the redirect url is to your root page. What you should do then, is to have some logic in your root page to check the user agent of the incoming request and route appropriately. I would check out http://detectmobilebrowsers.com/ for an open source code snippet to detect mobile browsers. You should put your root page URL in the first field, and the mobile version in the second.

Let me know if that helps.