[英]I can't catch Facebookexceptions in try catch block
I'm having an issue catching the proper exception, no matter what i do i get the same response. 我有一个问题是捕捉到正确的异常,无论我做什么,我得到相同的响应。 This is the response i get in the view Error validating access token: The user has not authorized application FacebookResponseException in FacebookResponseException.php line 89:
这是我在视图中得到的响应错误验证访问令牌:用户未在FacebookResponseException.php第89行中授权应用程序FacebookResponseException:
The issue arrises after a user de-authorizes my app from Facebook after have created an access_token , what I ant to do is catch the exception and automatically log them out and flush the session: But i can't seem to find the right exception to catch I've tested with all these: I narrowed the problem to being inside this catch block: I included the catch block, then the full code I'm using in my route's function after. 在用户在创建了access_token之后从Facebook取消授权我的应用程序之后,问题就出现了,我要做的就是捕获异常并自动将它们注销并刷新会话:但我似乎无法找到正确的例外catch我已经测试了所有这些:我将问题缩小到这个catch块内:我包含了catch块,然后是我在路由函数中使用的完整代码。
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=permissions');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookAuthenticationException $e) {
echo 'Facebook Dan1 returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookAuthorizationException $e) {
echo 'Facebook Dan2 returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookClientException $e) {
echo 'Facebook Dan3 returned an error: ' . $e->getMessage();
exit;
} catch(Exception $e) {
echo 'Facebook Dan4 returned an error: ' . $e->getMessage();
exit;
}
now my full code for this page: 现在我这个页面的完整代码:
public function getHomeProfile(Request $request, LaravelFacebookSdk $fb)
{
$user = Auth::user();
$token = Session::get('fb_user_access_token');
$twitterToken = Session::get('access_token');
// $fb->setDefaultAccessToken($token);
// $resp = $fb->get('/debug_token?=input_token=$token');
// dd($resp);
$permissionsToRequest = ([
'public_profile',
'publish_actions'
]);
$login_link_for_public_actions = $fb->getLoginUrl($permissionsToRequest, 'http://pms.dev:8000/facebook/publicactions/callback');
if (isset($token)) {
$fb->setDefaultAccessToken($token);
// $debugToken = $fb->get('/debug_token?input_token=' . $token);
// $debugTokenResponse = $debugToken->getGraphNode()->asArray();
// echo "<pre>";
// print_r($debugTokenResponse);
// echo "<pre>";
// die();
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=permissions');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookAuthenticationException $e) {
echo 'Facebook Dan1 returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookAuthorizationException $e) {
echo 'Facebook Dan2 returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookClientException $e) {
echo 'Facebook Dan3 returned an error: ' . $e->getMessage();
exit;
} catch(Exception $e) {
echo 'Facebook Dan4 returned an error: ' . $e->getMessage();
exit;
}
// Returns a `Facebook\GraphNodes\GraphUser` collection
$facebookuser = $response->getGraphUser();
//$ty= json_decode($facebookuser);
$permissions = $facebookuser['permissions'];
$checked = '';
foreach ($permissions as $p) {
if ($p['permission'] == 'publish_actions' && $p['status'] == 'granted' ) {
$checked = 'checked';
}
}
} else {
$checked = null;
}
if (isset($twitterToken)) {
$twitterChecked = 'checked';
} else {
$twitterChecked = null;
}
$userPlugsCountry = $user->plugsCountry()->setPath($request->url());
$user_plugs_list = Auth::user()->plugs()->lists('id');
if (Auth::check()) {
$statuses = Status::where(function($query) {
return $query->where('user_id', Auth::user()->id)->where('parent_id', NULL)
->orWhereIn('user_id', Auth::user()->plugs()->lists('id'));
})->orderBy('created_at', 'desc')->paginate(7);
}
if (array_key_exists('REQUEST_SCHEME', $_SERVER)) {
$cors_location = $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"] .
dirname($_SERVER["SCRIPT_NAME"]) . "/cloudinary_cors.html";
} else {
$cors_location = "http://" . $_SERVER["HTTP_HOST"] . "/cloudinary_cors.html";
}
if ($request->ajax()) {
return [
'statuses' => view('ajax.status')->with('user', $user)->with(compact('statuses'))->render(),
'next_page' => $statuses->nextPageUrl(),
'countries' => view('ajax.next_countries')->with('user', $user)->with(compact('userPlugsCountry'))->render(),
'next_countries_page' => $userPlugsCountry->nextPageUrl(),
'prev_countries_page' => $userPlugsCountry->previousPageUrl(),
];
}
return view('profile.home')->with('user', $user)->with('cors_location', $cors_location)->with('statuses',$statuses)->with('userPlugsCountry',$userPlugsCountry)->with('checked', $checked)->with('login_link_for_public_actions',$login_link_for_public_actions)->with('twitterChecked', $twitterChecked);
}
OK i figured out whet i was doing wrong. 好吧我发现我做错了。 I'm using sammyk laravelfacebooksdk https://github.com/SammyK/LaravelFacebookSdk and was only including:
我正在使用sammyk laravelfacebooksdk https://github.com/SammyK/LaravelFacebookSdk并且仅包括:
use SammyK\LaravelFacebookSdk\LaravelFacebookSdk;
I didn't realize i also needed to add 我没有意识到我还需要添加
use Facebook;
to the namespace. 到命名空间。
easy fix, that gave me a lot of trouble. 容易修复,这给了我很多麻烦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.