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

15,739

Solution 1

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...

Solution 2

Why don't you try the Facebook authenticated referrals? It's the proper solution for such problem. Since it asks Facebook to traps the non-connected users and redirect them directly to the connect page.

This insure that all of the users are connected to your app by default.

Solution 3

Here's another option:

<script>
    (function () {
        var url = 'your_permission_url';
        window.top.location.href = url;
    } ());
</script>

Solution 4

The url should be from getLoginUrl($PARAMS). It worked for me. In your case,

$PARAMS = array('scope' => 'user_photos,publish_stream', redirect_uri => 'https://apps.facebook.com/myappxxxxx');

Share:
15,739
Scott Szretter
Author by

Scott Szretter

As. Director or Information Technology / Developer N1VAN www.508tech.com Apache / PHP / MySQL IIS / ASP.NET / MS-SQL Flex / Flash Builder / Actionscript .NET / C# / VB / MVC JavaScript / XML / HTML / DHTML Java / Objective-C Enterprise Networking / Server &amp; Workstation Hardware / Storage VMWARE / VEEAM

Updated on June 13, 2022

Comments

  • Scott Szretter
    Scott Szretter almost 2 years

    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.

  • Scott Szretter
    Scott Szretter over 13 years
    Finally, YES, this worked. I also used the PHP api to get the loginurl : $loginUrl = $facebook->getLoginUrl(); and set $url to that.
  • Scott Szretter
    Scott Szretter over 13 years
    Question - is there a difference / advantage between using window.top.location.href vs. window.open (_parent) ?
  • Scott Szretter
    Scott Szretter over 13 years
    FYI - I had to build the URL manually instead of using getLoginUrl as it was redirecting to my site instead of the fb apps url.
  • demux
    demux over 13 years
    I guess window.top.location.href should work just the same. And yes, I also found out the hard way that $facebook->getLoginUrl(); is useless.
  • Scott Szretter
    Scott Szretter over 13 years
    I figured out later you can use getLoginUrl() as long as you specify the 'next' and other url's and put in your apps.facebook... url
  • demux
    demux over 13 years
    Cool, can you post your new solution as another answer to your question, please? It might be of some use for me in the future. ;)
  • Lix
    Lix over 12 years
    getSession() is the old method, you should be using getUser()
  • 0plus1
    0plus1 about 12 years
    This is the real answer. The most voted one is an hack and as such may result in a temporary ban.
  • MeetM
    MeetM almost 11 years
    Facebook removed Facebook authenticated referrals. Too bad!