Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook - how to go directly to the request permission dialog with an iframe app?

When a user accesses my facebook app via http://apps.facebook.com/myappxxxxx, I want it to immediately show the request permissions fb dialog - like so many others do.

I have tried all sorts of things - including php redirect using the getLoginUrl, javascript window location redirect using the same.

The problem is that it shows a big facebook icon instead of the request permissions. However, if you click on that icon, it in fact goes to the correct request permissions page!

Does anyone know how to redirect to the facebook permissions page properly?

I am using the PHP api and JavaScript SDK with an iFrame FB app.

like image 810
Scott Szretter Avatar asked Dec 07 '10 21:12

Scott Szretter


1 Answers

I had the exact same problem a while ago. Here's a little hack:

if(!$me) {  
    $url =   "https://graph.facebook.com/oauth/authorize?"
                ."client_id=YOUR_APP_ID&"
                ."redirect_uri=http://apps.facebook.com/APP_SLUG/&"
                ."scope=user_photos,publish_stream";
    echo "<script language=javascript>window.open('$url', '_parent', '')</script>";
} else {
    your code...
}

How it looks in my code (a bit dirty, I had a deadline):

require_once 'facebook-php-sdk/src/facebook.php';

$appid = '';

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

$session = $facebook->getSession();

$me = null;
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}
if(!$me) {
    // here we redirect for authentication:
    // This is the code you should be looking at:
    $url =   "https://graph.facebook.com/oauth/authorize?"
            ."client_id=$appid&"
            ."redirect_uri=http://apps.facebook.com/APP_SLUG/&"
            ."scope=user_photos,publish_stream";
    ?>
    <!doctype html>
    <html xmlns:fb="http://www.facebook.com/2008/fbml">
        <head>
            <title>Göngum til góðs</title>
            <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
            <script language=javascript>window.open('<?php echo $url ?>', '_parent', '')</script>
        </head>

        <body>
            Loading...
        </body>
    </html>

    <?php
    exit;
} else {
    your code ...
}

A simple header redirect won't work since it will only redirect your iframe. Javascript is needed to access the _parent window.

Edit

Keep in mind that there is a proper way to do this (see answer below) and I do not recommend doing this. But hey, what ever floats your boat...

like image 172
demux Avatar answered Sep 21 '22 14:09

demux