用户拒绝请求后尝试请求权限时出现错误消息(使用 Facebook PHP SDK)
我正在使用 PHP Facebook SDK (v.3.1.1) 来构建我的 Facebook 应用程序。 在我的流程中的某个时刻,我需要用户接受我的应用程序。我使用以下代码:
// Is there an error? Redirect to explain permissions page
if(!empty($_GET['error'])) {
$redirectUrl = APP_FACEBOOK_PAGE;
require_once(ABSOLUTE_DOCUMENTROOT_PATH.'do_redirect.inc.php');
die();
}
// Prepare app
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie'=> true
));
$facebookId = $facebook->getUser();
// Changed user?
if((!empty($_SESSION['facebookAuth']['facebookId']) && $_SESSION['facebookAuth'] ['facebookId'] != $facebookId)) {
$facebook->setAccessToken('');
$_SESSION['facebookAuth']['facebookId'] = $facebookId;
$redirectUrl = getRequestPermissionsUrl($facebook);
require_once(ABSOLUTE_DOCUMENTROOT_PATH.'do_redirect.inc.php');
die();
}
// No access token? Re-acquire it!
if($facebook->getAccessToken() == '') {
$redirectUrl = getRequestPermissionsUrl($facebook);
require_once(ABSOLUTE_DOCUMENTROOT_PATH.'do_redirect.inc.php');
die();
}
// Let's see if we have an active session
if(!empty($_SESSION['facebookAuth']['facebookId']) && !empty($_SESSION['facebookAuth'] ['facebookUserObject'])){
$facebookId = $_SESSION['facebookAuth']['facebookId'];
$facebookUserObject = $_SESSION['facebookAuth']['facebookUserObject'];
}else{
$facebookId = $facebook->getUser();
if ($facebookId) {
try {
$facebookUserObject = $facebook->api('/me');
// Put them in session so we can re-use them
$_SESSION['facebookAuth']['facebookId'] = $facebookId;
$_SESSION['facebookAuth']['facebookUserObject'] = $facebookUserObject;
} catch (FacebookApiException $e) {
error_log($e);
}
}
}
// If no facebookId -> check permissions anyway
if(empty($facebookId)) {
$redirectUrl = getRequestPermissionsUrl($facebook);
require_once(ABSOLUTE_DOCUMENTROOT_PATH.'do_redirect.inc.php');
die();
}
我的函数“getRequestPermissionsUrl”调用 getLoginUrl:
return $redirectUrl = $facebookObject->getLoginUrl(array('canvas'=>1,'fbconnect'=>0,'redirect_uri'=>$returnUrl,'scope'=>'publish_stream,email,user_likes'));
文件“do_redirect.inc.php”执行 js 重定向:
top.location.href = '$redirectUrl';
因此,考虑到这一点,让我们看一下第一部分。 在用户拒绝权限并重新加载页面后,此代码将再次包含在内。在这种情况下,$_GET['error'] 被填充,我们将被重定向到应用程序中的另一个 php 文件 - 该文件不包含我发布的代码,但与权限或内容无关。在另一个文件中,有一个指向再次需要权限的页面的链接 - 上面的代码会再次加载,但是当尝试请求权限时,我在 Facebook 页面上收到错误“出现问题,我们正在尝试修复它” 。
我已经尝试了一切,从销毁会话到将 accestoken 设置为空,再到......你能想到的。我现在完全没有想法,所以如果你们有人有任何建议,我会很高兴。任何帮助将不胜感激。
提前致谢!
I'm using the PHP Facebook SDK (v.3.1.1) for building my Facebook app.
At some point in my flow I need the user to accept my app. I'm using the following code:
// Is there an error? Redirect to explain permissions page
if(!empty($_GET['error'])) {
$redirectUrl = APP_FACEBOOK_PAGE;
require_once(ABSOLUTE_DOCUMENTROOT_PATH.'do_redirect.inc.php');
die();
}
// Prepare app
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie'=> true
));
$facebookId = $facebook->getUser();
// Changed user?
if((!empty($_SESSION['facebookAuth']['facebookId']) && $_SESSION['facebookAuth'] ['facebookId'] != $facebookId)) {
$facebook->setAccessToken('');
$_SESSION['facebookAuth']['facebookId'] = $facebookId;
$redirectUrl = getRequestPermissionsUrl($facebook);
require_once(ABSOLUTE_DOCUMENTROOT_PATH.'do_redirect.inc.php');
die();
}
// No access token? Re-acquire it!
if($facebook->getAccessToken() == '') {
$redirectUrl = getRequestPermissionsUrl($facebook);
require_once(ABSOLUTE_DOCUMENTROOT_PATH.'do_redirect.inc.php');
die();
}
// Let's see if we have an active session
if(!empty($_SESSION['facebookAuth']['facebookId']) && !empty($_SESSION['facebookAuth'] ['facebookUserObject'])){
$facebookId = $_SESSION['facebookAuth']['facebookId'];
$facebookUserObject = $_SESSION['facebookAuth']['facebookUserObject'];
}else{
$facebookId = $facebook->getUser();
if ($facebookId) {
try {
$facebookUserObject = $facebook->api('/me');
// Put them in session so we can re-use them
$_SESSION['facebookAuth']['facebookId'] = $facebookId;
$_SESSION['facebookAuth']['facebookUserObject'] = $facebookUserObject;
} catch (FacebookApiException $e) {
error_log($e);
}
}
}
// If no facebookId -> check permissions anyway
if(empty($facebookId)) {
$redirectUrl = getRequestPermissionsUrl($facebook);
require_once(ABSOLUTE_DOCUMENTROOT_PATH.'do_redirect.inc.php');
die();
}
My function 'getRequestPermissionsUrl' calls for the getLoginUrl:
return $redirectUrl = $facebookObject->getLoginUrl(array('canvas'=>1,'fbconnect'=>0,'redirect_uri'=>$returnUrl,'scope'=>'publish_stream,email,user_likes'));
the file 'do_redirect.inc.php' does a js-redirect:
top.location.href = '$redirectUrl';
So, with that in mind let's take a look at the first part.
This code gets included again after the user has denied the permission and the page reloads. In that case $_GET['error'] is filled and we'll be redirected to another php-file in the app - a file that does not include the code I posted but has nothing to do with permissions or stuff at all. In that other file is a link to the page that needs permission again - the code above gets loaded again but when trying to request permissions I'm getting the error 'something went wrong and we're trying to fix it' on a Facebook page.
I've tried evertyhing from destroying sessions to setting the accestoken empty to... you name it. I'm quite out of ideas now so I'd be glad if somebody of you guys have any suggestions. Any help would be greatly appreciated.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚看到一个新问题问同样的事情 - 抱歉,在 SDK 3.0 上取消应用程序连接后出现问题
有一个 Facebook 错误跟踪器中当前未解决的错误
I just saw a newer question asking the same thing - Sorry, something went wrong after cancel application connexion on SDK 3.0
There is a currently open bug in Facebook's bug tracker about this