laravel / socialite

Laravel wrapper around OAuth 1 & OAuth 2 libraries.
https://laravel.com/docs/socialite
MIT License
5.57k stars 940 forks source link

Client error - missing required "code" parameter #515

Closed lbreza closed 3 years ago

lbreza commented 3 years ago

Description:

I know there were already some of the issues (#294) related to this but this still haven't been fixed and it's bothering me for some time because I can't make it work.

It may be problem only for the specific cases but in some cases it's not working.\ If I'm wrong please correct me.

Problem is with Google provider because it returns the exception when you want to handle the callback and retrieve user details.

Socialite::driver('google')->stateless()->user()

returns

GuzzleHttp\Exception\ClientException Client error: POST https://www.googleapis.com/oauth2/v4/token resulted in a 400 Bad Request response: { "error": "invalid_request", "error_description": "Missing required parameter: code" }

111

However it works normally if you test it locally (localhost).

You can make it work on live server (production) only when you remove 'profile' from $scopes at socialite/src/Two/GoogleProvider.php but you don't receive user details such as given_name, family_name, ... which are needed.

Steps To Reproduce:

  1. Make sure you're testing it on live server (production) and not locally (localhost).
  2. At socialite/src/Two/GoogleProvider.php file set
    protected $scopes = [
    'openid',
    'profile',
    'email',
    ];
  3. Try to login with Google account.
  4. Check if Socialite::driver('google')->stateless()->user() works and you get all the user information when you handle the callback.
driesvints commented 3 years ago

However it works normally if you test it locally (localhost).

This just leads me to believe there's something wrong with your setup in production.

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to report back and I'll gladly help you out and re-open this issue.

Thanks!

lbreza commented 3 years ago

However it works normally if you test it locally (localhost).

This just leads me to believe there's something wrong with your setup in production.

Can you first please try one of the support channels below? If you can actually identify this as a bug, feel free to report back and I'll gladly help you out and re-open this issue.

* [Laracasts Forums](https://laracasts.com/discuss)

* [Laravel.io Forums](https://laravel.io/forum)

* [StackOverflow](https://stackoverflow.com/questions/tagged/laravel)

* [Discord](https://discordapp.com/invite/KxwQuKb)

* [Larachat](https://larachat.co)

* [IRC](https://webchat.freenode.net/?nick=laravelnewbie&channels=%23laravel&prompt=1)

Thanks!

You were right.

After additional debugging and researching I figured out that it was problem with server ModSecurity.\ It was blocking the Google callback request...

Thank you!

Hellmuth99 commented 5 months ago

@lbreza Could you solve it in the end? I have the same error

lbreza commented 5 months ago

@lbreza Could you solve it in the end? I have the same error

Yes. In my case it was ModSecurity (web based firewall) on production server which was blocking the requests for Google API.

Disabling some of the ModSecurity rules worked in the end.

emf-developer commented 1 month ago

In my case, the error occurred when user declined to accept google agreement and google redirected user to the callback url with error query parameter and I forgot to check error and continued to get user using socialite package.

Untitled

Example response by callback: https://example.com?error=access_denied&state=blahblah

khoirulyahya commented 1 month ago

yes sir thanks for your answer @emf-developer . Before i know that, my code like this.

$user = Socialite::driver($provider)->stateless()->user(); $existingUser = User::where('email', $user->email)->first();

on my history error happen in method user() after stateless() like your answer, the problem happen when user cancel sign in to process

so we apply try catch like this try { $user = Socialite::driver($provider)->stateless()->user(); } catch (\Exception $e) { return redirect()->to('https://blablabla.id/login'); }

and it solved. So, thank you sir