I’m using stateless fromUserToken
and this is the error I’m getting:
"errors": {
"error": "Client error: `GET https://www.googleapis.com/oauth2/v3/userinfo?prettyPrint=false` resulted in a `401 Unauthorized` response:n{n "error": "invalid_request",n "error_description": "Invalid Credentials"n}n",
"code": 401
}
this is my endpoint that I use, it worked with all the sections I have in
public function authenticate (Request $request, $provider) {
//Validate provider
// Authenticate
$userSocial = null;
try {
if ($provider == 'twitter') {
$userSocial = Socialite::driver('twitter')->userFromTokenAndSecret($request->token, $request->token_secret);
} else {
$accessToken = $request->token;
$userSocial = Socialite::driver($provider)->stateless()->userFromToken($accessToken);
}
} catch (Exception $e) {
return $this->respondError('Login error',[
'error' => $e->getMessage(),
'code' => $e->getCode()
],401);
}
$user = User::where([
'provider' => $provider,
'provider_id' => $userSocial->id
])->first();
if($user){
$token = $user->createToken('api')->plainTextToken;
return $this->respondWithToken($token, 'Logged in successfully', false);
} else {
$user = User::create([
'name' => $userSocial->getName() ?: 'Apple User',
'email' => $userSocial->getEmail(),
'image' => $userSocial->getAvatar(),
'provider_id' => $userSocial->getId(),
'provider' => $provider,
]);
$token = $user->createToken('api');
return $this->respondWithToken($token, 'Registered successfully', true);
}
}
Any Idea how to solve it?
and this is the code I used
2
Answers
The token that I was getting was jwt token, not access token. so It had problems with decoding it with stateless socialite function.
when requesting for authorization, you should set the
response_type
andscope
url parameter like below: