Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle laravel socialite "Missing authorization Exception"

In Laravel Socialite We are redirected to facebook. But When User Cancels (not allowing facebook to access public profile) it is giving error Missing Authorization exception

ClientException in RequestException.php line 107: Client error: GET https://graph.facebook.com/oauth/access_token?client_id=1309844325833234&client_secret=1304bbdd28400tret49a295d324d577c&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fauth%2Ffacebook%2Fcallback` resulted in a 400 Bad Request response: {"error":{"message":"Missing authorization code","type":"OAuthException","code":1,"fbtrace_id":"Aq9wMwG6ewl"}}

I dont want to display this Instead I Want to return to my site home page by giving a message "Facebook Login Failed" like what shown in stackoverflow facebook login.

like image 449
Satyanarayana Swamy Avatar asked Mar 13 '16 16:03

Satyanarayana Swamy


2 Answers

Finally i got answer.Here it is

public function handleProviderCallback()
{
    try {
        $user = Socialite::driver('facebook')->user();
    } catch (\Exception $e) {
        //Here you can write excepion Handling Logic
    }
}
like image 60
Satyanarayana Swamy Avatar answered Oct 07 '22 05:10

Satyanarayana Swamy


Try catch didn't give good result to me. I used below methods to catch this error. If you're using Laravel Socialite library definitely it has function call handleProviderCallback. In that use this code

handleProviderCallback Method

/**
 * Obtain the user information from GitHub.
 *
 * @return Response
 */
public function handleProviderCallback()
{

    $error_code = Input::get('error_code');

    if ($error_code == 200)
    {
        return redirect()->route('sign-in')->with('error','You\'ve chose not to grant us permission to connect with your Facebook account.');
    }
    else
    {
        $fbUser = Socialite::driver('facebook')->user();
        # rest of your code
    }
}

Where does this error_code come from ??

Well, If you look at the error page(Laravel Black screened) come to you check the URL of that page. It has the these get methods error , error_code , error_description , error_reason , state.

Ex : http://localhost:8000/login/facebook/callback?error=access_denied&error_code=200&error_description=Permissions+error&error_reason=user_denied&state=mIxNjoDCogT2piMV5LX1Imk6GWNzqPUt3JZaqsIo#_=_


What I can do this to optimize this

You can use a switch statement based on error Check this Facebook error codes, with an error message and pass it.

like image 1
Abdulla Nilam Avatar answered Oct 07 '22 05:10

Abdulla Nilam