SammyK / LaravelFacebookSdk

Fully unit tested Facebook SDK v5 integration for Laravel & Lumen
MIT License
693 stars 201 forks source link

I can't catch Facebookexceptions in try catch block #126

Closed GQwebsites closed 8 years ago

GQwebsites commented 8 years ago

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:

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 commented 8 years ago

Hey @GQwebsites! You're already catching the FacebookResponseException. You just need a method to do everything you mentioned after you catch the exception:

try {
    $response = $fb->get('/me?fields=permissions');
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
    $this->doOtherStuff();
    return;
}
GQwebsites commented 8 years ago

@SammyK screen shot 2016-08-05 at 3 43 14 pm Thanks, but the problem I'm facing is that no matter what I do after i catch the exception, the code I put in the catch block won't run. here's what i did to test:

try { $response = $fb->get('/me?fields=permissions'); } catch(\Facebook\Exceptions\FacebookResponseException $e) { dd('Test Run'); return; }

and i still get the same response : FacebookResponseException in FacebookResponseException.php line 89: Error validating access token: The user has not authorized application

I know why the exceptionr is getting thrown, its after a user de-authorizes my app. What I can't seem to do is trigger a method to run inside my catch block if the FacebookResponseException is hit.

GQwebsites commented 8 years ago

@SammyK OK I solved it, i didn't know i needed to add use Facebook; to the namespace, all i added was use SammyK\LaravelFacebookSdk\LaravelFacebookSdk;

its all working good now, not sure if mentioned it in the documentation, if you did I must of missed it.