5

I have a problem in my production environment with Laravel Socialite's Google auth. The error is same as on this post:

laravel socialite not working on live server, only works on local machine

but I have an Apache server and after many tries, I haven't found a solution.

Edit:

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"
}
cercxtrova
  • 1,555
  • 13
  • 30
Vaunt
  • 106
  • 1
  • 7
  • what kind of error do you receive? – ege Nov 25 '19 at 12:10
  • Yes, sorry, the same in the link, i receive that :"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"↵}↵ – Vaunt Nov 25 '19 at 12:14
  • 1
    Does this answer your question? [Laravel socialite 400 Bad Request response](https://stackoverflow.com/questions/48616799/laravel-socialite-400-bad-request-response) – N69S Nov 25 '19 at 13:20
  • thx but not is not the same problem – Vaunt Nov 25 '19 at 13:25
  • is it working when running `php artisan serve`? – ege Nov 25 '19 at 15:11
  • Its work on local with Mamp, same with php artisan serve – Vaunt Nov 25 '19 at 15:27

4 Answers4

1
    public function redirectToProvider($driver)
{
  //  return Socialite::driver($driver)->stateless()->redirect();
    return Socialite::driver('google') ->setScopes(['openid', 'email'])

        ->redirect();
}
  • 1
    While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn, and apply that knowledge to their own code. You are also likely to have positive feedback from users in the form of upvotes, when the code is explained. – Amit Verma Jan 28 '21 at 17:18
  • if I add third parameter to scope it return error to me. can you plz tell how to get name using scope..? `setScopes(['openid', 'email', 'name'])` – Neeraj Tangariya Jul 17 '21 at 08:22
0

This issue occurred due to profile scope in vendor\laravel\socialite\src\Two\GoogleProvider.php .

You can overcome this error by following 2 ways:

  1. Remove OR comment profile scope in vendor\laravel\socialite\src\Two\GoogleProvider.php
    protected $scopes = [
        'openid',
        'email',
    ];
  1. Initiate the Google OAUth2 stateless();
    return Socialite::driver('google')->stateless()->redirect();
0

See this answer if you are running an Nginx server.

Run sudo nano /etc/nginx/sites-available/default or your site config file

and fixed this line:

try_files $uri $uri/ /index.php?query_string; // wrong

to

try_files $uri $uri/ /index.php?$query_string; // fixed

Tatha
  • 127
  • 1
  • 14
0

check first your redirect url is correct and run command:

php artisan passport:install

see this below snippet may help you

 public function socialRedirect($provider)
  {
    if ($provider === 'clever') {
      $clientId = Config::get('services.clever.client_id');
      $redirect = Config::get('services.clever.redirect');
      return redirect()->away('https://clever.com/oauth/authorize?response_type=code&redirect_uri=' . $redirect . '&client_id=' . $clientId);
    } else {
      return Socialite::driver($provider)->redirect();
    }
  }

  public function socialCallback($provider)
  {
    if ($provider === 'apple') {
      $token = app(Configuration::class)->parser()->parse(app(AppleToken::class)->generate());
      config()->set('services.apple.client_secret', $token);
    }

    $socialUser = Socialite::driver($provider)->setHttpClient(new Client(['verify' => false]))->user();

    $userIdentity = UserIdentity::where('provider_id', $socialUser->id)->where('provider_name', $provider)->first();
    $userRole = UserRole::where('slug', 'user')->first();

    if ($userIdentity) {
      // retrieve the user from users store
      $user = User::where('id', $userIdentity->user_id)->with('userRole')->with('account')->first();

      // assign access token to user
      $token = $user->createToken('social');
      $accessToken = $token->accessToken;

      $arguments = [
        'success' => true,
        'accessToken' => $accessToken,
        'expiresAt' => Carbon::parse($token->token->expires_at)->toDateTimeString(),
        'user' => json_encode($user)
      ];

      return redirect()->away(env('CLIENT_URL') . '/social/callback?' . http_build_query($arguments));
    } else {
      $user = User::where('email', $socialUser->email)->with('userRole')->with('account')->first();

      if (!($user && isset($user->id))) {
        /* $newUser = User::create([
          'fname' => $socialUser->name ?? '',
          'lname' => '',
          'email' => $socialUser->email,
          'image' => $socialUser->avatar ?? '',
          'user_role_id' => $userRole->id,
          'account_id' => 1,
          'password' => Hash::make(Str::random(40)),
          'status' => 'active',
        ]);

        $user = User::where('email', $socialUser->email)->with('userRole')->with('account')->first(); */

        $arguments = [
          'success' => false,
        ];

        return redirect()->away(env('CLIENT_URL') . '/social/callback?' . http_build_query($arguments));
      } else {
        // store user social provider info
        if ($user) {
          UserIdentity::create([
            'provider_name' => $provider,
            'provider_id' => $socialUser->id,
            'user_id' => $user->id,
          ]);
        }

        // assign passport token to user
        $token = $user->createToken('social');
        $accessToken = $token->accessToken;

        $arguments = [
          'success' => true,
          'accessToken' => $accessToken,
          'expiresAt' => Carbon::parse($token->token->expires_at)->toDateTimeString(),
          'user' => json_encode($user)
        ];

        return redirect()->away(env('CLIENT_URL') . '/social/callback?' . http_build_query($arguments));
      }
    }