在FB.ui中使用方法permissions.request重定向uri无法正常工作

时间:2011-02-28 09:51:01

标签: php facebook iframe fbjs facebook-php-sdk

您好,我最近修改了我的应用程序,它只需要基本信息。用户的许可,但现在我也想要流发布权限。所以我检查我的索引页面如果用户没有被授予流发布权限我只是显示他的权限对话框如下:

<?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>";
   }
?>

此代码正确显示权限框,但问题是当用户授予权限时,他会重定向到我的画布URL(服务器页面上的url)而不是画布页面(即http://apps.facebook.com/xyz)。为了解决这个问题,我将redirect_uri添加为

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

但它仍无效。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:4)

请改为尝试:

<?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>");
?>

您还可以使用FQL检查权限。有关这方面的更多信息,请here


<强>更新
Facebook引入了permissions连接,现在是it can be used而不是旧的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")) );
}