我无法在尝试捕获块中捕获Facebookexceptions [英] I can't catch Facebookexceptions in try catch block

查看:71
本文介绍了我无法在尝试捕获块中捕获Facebookexceptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我做什么我都遇到同样的问题,但是我遇到了捕获适当异常的问题.这是我认为的回应验证访问令牌时出错:用户未在FacebookResponseException.php第89行中授权应用程序FacebookResponseException:

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:

在用户创建了access_token后从Facebook取消对我的应用程序的授权后,该问题浮出水面,我想要做的是捕获异常并自动将其注销并刷新会话:但是我似乎找不到正确的例外我已经对所有这些进行了测试:我将问题范围缩小到了这个catch块内:我包括了catch块,然后是我在路由函数中使用的完整代码.

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.

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);
 }

推荐答案

好的,我发现我做错了什么.我正在使用sammyk laravelfacebooksdk https://github.com/SammyK/LaravelFacebookSdk ,并且仅包括:

OK i figured out whet i was doing wrong. I'm using sammyk laravelfacebooksdk https://github.com/SammyK/LaravelFacebookSdk and was only including:

use SammyK\LaravelFacebookSdk\LaravelFacebookSdk;

我没有意识到我也需要添加

I didn't realize i also needed to add

use Facebook;

到名称空间.

容易修复,这给我带来了很多麻烦.

easy fix, that gave me a lot of trouble.

这篇关于我无法在尝试捕获块中捕获Facebookexceptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆