问题
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(); // "anzhengchao@gmail.com"
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
"""
回答1:
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:
- User click the login button on your website
- You redirect user to Google and Google is asking user to login/grant you access
- If successful, Google redirects back to you with a one-time-use Authentication Code
?code=.....
- Socialite use the
?code
and exchange it with Google to get user's Access Token. This can only be done once per flow. - You can now request user details using the access token requested in step 4.
Read similar answer: https://stackoverflow.com/a/32710034/534862
来源:https://stackoverflow.com/questions/48616799/laravel-socialite-400-bad-request-response