In FB.ui with method permissions.request redirect uri not working properly

3.3k Views Asked by At

Hi I modify my application recently first it just takes basic info. permission from users but now I want stream publish permission too. So I check on my index page if users not granted stream publish permission I just show him permission dialog box as follows:

<?php $permission = $facebook->api(array('method' =>   'users.hasAppPermission','ext_perm'=>'publish_stream','uid'=> $uid));
   if($permission != '1')
   {
    echo "<script type='text/javascript'>

                var dialog = {
                    method: 'permissions.request',
                    perms: 'publish_stream'
                };  

            FB.ui(dialog,null);
        </script>";
   }
?>

This code shows permission box properly but problem is that when user grants permission he is redirect to my canvas url (url on servers page) and not on canvas page(i.e. http://apps.facebook.com/xyz). To solve this problem I added redirect_uri to it as

   var dialog = {
       method: 'permissions.request',
       perms: 'publish_stream',
       redirect_uri: 'http://apps.facebook.com/xyz'
   };

but still it's not working.

Please help me how to solve this problem.

1

There are 1 best solutions below

0
On

Try this instead:

<?php
$loginUrl = $facebook->getLoginUrl(array(
    "scope" => "publish_stream",
    "redirect_uri" => "http://apps.facebook.com/xyz"
));

$isGranted = $facebook->api(array(
    "method"    => "users.hasAppPermission",
    "ext_perm"   => "publish_stream",
    "uid"       => $uid /* The user ID of the user whose permissions
                         * you are checking. If this parameter is not
                         * specified, then it defaults to the session user.
                         */
));
if($isGranted !== "1")
    echo("<script> top.location.href='" . $loginUrl . "'</script>");
?>

You can also use FQL to check for the permission. More about that can be found here.


UPDATE:
Facebook introduced the permissions connection and now it can be used instead of the old REST API:

$permissions = $facebook->api("/me/permissions");
if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
    // Permission is granted!
    // Do the related task
    $post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!'));
} else {
    // We don't have the permission
    // Alert the user or ask for the permission!
    header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );
}