Facebook API:获取用户的电子邮件权限,避免CANCEL循环



我正在开发一个facebook应用程序。我正在服务一个网页,这将是一个facebook粉丝页面内的iFrame。问题是,当用户单击cabncel(不允许)按钮(它循环)时,我无法处理这种情况。

谁能给我一个简单的例子如何处理请求许可?假设我只需要访问用户的电子邮件,我想在用户点击取消时重定向到页面墙,这样就不会进入循环。

这是来自控制器

的一些代码
$this->facebook = Zend_Registry::get('facebook');
$signed_request = $this->facebook->getSignedRequest();
$this->view->hasAllowed = isset($signed_request['oauth_token']) ? true : false;

,这是来自视图:

<?php if(!$this->hasAllowed): ?>
<script type="text/javascript">
   top.location.href = '<?= $this->oauth_url ?>';
</script>
<?php endif;?>
<div id="fb-root"></div>
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
    FB.init({
      appId  : '<?=$this->app_id?>',
      status : true, // check login status
      cookie : true, // enable cookies to allow the server to access the session
      xfbml  : true  // parse XFBML
    });
</script>

From the docs:

如果用户按下不允许,你的应用程序未被授权。OAuth对话框将重定向(通过HTTP 302)用户的浏览器到你在redirect_uri参数中传递的URL,并提供以下错误信息:http://YOUR_URL?error_reason=user_denied&错误= access_denied& error_description = +用户+否认+ +请求。

所以你目前的问题是,如果用户点击"不允许",你正在显示相同的视图,其中包括相同的JS登录代码,重新启动进程。你所需要做的就是检查你的回调操作的error_reason参数,如果存在并设置为user_denied,重定向到应用程序墙页面:

public function callbackAction()
{
    if ($this->_hasParam('error_reason') && $this->_getParam('error_reason') == 'user_denied') {
        $this->_helper->Redirector->setGotoUrl('http://example.com/yourapppage');
    }
}

或者你可以呈现一个不同的视图,向用户解释为什么你的应用程序需要它所拥有的权限,并给他们一个再次登录的选项。

我看不出你的$this->oauth_url是什么,但它必须是这样的:

top.location.href="http://www.facebook.com/connect/uiserver.php?app_id=APP_ID&method=permissions.request&display=page&next=URL_TO_GO_WHEN_USER_ACCEPT&cancel_url=URL_TO_GO_WHEN_USER_CANCEL&response_type=token&canvas=1&perms=email";

可能,你需要的是cancel_url参数,重定向用户到这个url,如果他不允许。

希望有帮助!

相关内容

最新更新