Madhu Nair
Madhu Nair

Reputation: 436

Laravel socialite 400 Bad Request response

i have created a laravel socialte setup .and it was working before perfectly now its showing error(below). 1)i have changed client_secret 2)created a new oauth credentials still not working

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



         public function handleGoogleCallback()
        {

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


                $user->getId();        // 1472352
                $user->getNickname();  // "overtrue"
                $name= $user->getName();      // "安正超"
                $emailid= $user->getEmail(); 
                $pic= $user->getAvatar();    // "[email protected]"
return->redirect('welcome');

}

i have created env file with client_secret and client id

  """
    Client error: `POST https://accounts.google.com/o/oauth2/token` resulted in a `400 Bad Request` response:\n
    {\n
      "error" : "invalid_grant",\n
      "error_description" : "Code was already redeemed."\n
    }\n
    """

Upvotes: 2

Views: 14002

Answers (1)

Lionel Chan
Lionel Chan

Reputation: 8069

When Google return the Authentication Code code to your Socialite, it can only be used to exchange to Access Token once. Doing more than once will result in the error Code was already redeemed.

The flow should be:

  1. User click the login button on your website
  2. You redirect user to Google and Google is asking user to login/grant you access
  3. If successful, Google redirects back to you with a one-time-use Authentication Code?code=.....
  4. Socialite use the ?code and exchange it with Google to get user's Access Token. This can only be done once per flow.
  5. You can now request user details using the access token requested in step 4.

Read similar answer: https://stackoverflow.com/a/32710034/534862

Upvotes: 4

Related Questions