Laravel Socialite: Oauth 2 Rejection Workflow

631 views Asked by At

I am trying out the "reject" workflow when a user cancels the OAuth Authorization request from the Social (LinkedIn in this case) page

ClientException in RequestException.php line 111: Client error: POST https://www.linkedin.com/oauth/v2/accessToken resulted in a 400 Bad Request response: {"error":"missing_parameter","error_description":"A required parameter \"code\" is missing"}

Controllers have the standard code:

public function redirect()
{
    return Socialite::driver('linkedin')->redirect();
}

public function callback()
{
    $providerUser = Socialite::driver('linkedin')->user();
    ...
}

It works fine if the user clicks Allow/Accept on the social site. It throws exception when the user cancels.

I am unable to figure out how to handle the user's "Reject" scenario.

2

There are 2 answers

0
Saumini Navaratnam On

In the callback function add a try catch block for exception and do whatever you like to do.

try {

    $user = Socialite::driver('facebook')->user();

} catch (\Exception $e) {
    return redirect('redirect'); // This will ask for access from user again. I don't prefer
    // Do whatever you like
}
0
Rezoanul Alam Riad On

After a long search, the solution is to remove the 'r_liteprofile' from scopes and put 'r_basicprofile' in vendor\laravel\socialite\src\Two\LinkedInProvider.php

It should be: protected $scopes = ['r_basicprofile', 'r_emailaddress'];