3
votes

I have an issue with Socialite authentication via Google. I have two separate apps: Laravel and React Native. For react native app I use @react-native-community/google-signin and after getting a token on the client I'm sending it to the Laravel app, where I pass this token into Socialite function: Socialite::driver('google')->userFromToken($token); . I get this error:

Client error: GET https://www.googleapis.com/oauth2/v3/userinfo?prettyPrint=false resulted in a 401 Unauthorized response:
{
  "error": "invalid_request",
  "error_description": "Invalid Credentials"
}

I've rechecked credentials 4 times and I'm sure they are right. I use the same client id as in react native app. What am I doing wrong?

Note: I am using ID token to authorise instead of auth token.

1
I am assuming you are trying to implement one-tap sign in (EDIT: missed the google-signin package in your post sorry). When socialite calls to https://www.googleapis.com/oauth2 it is expecting an access token and not an ID token.Logan B. Lehman

1 Answers

1
votes

From my research on a similar issue - It looks like Google one tap doesn't work in the same way as oAuth does. So, you'd have to fetch a user from the $token manually, using the google/apiclient package.

There's an example in Google docs (it's in the android-section, but these particular snippets refer to back-end part, so it should still do the trick)

composer require google/apiclient


// Get $id_token via HTTPS POST.

$client = new Google_Client(['client_id' => $CLIENT_ID]);  // Specify the CLIENT_ID of the app that accesses the backend
$payload = $client->verifyIdToken($id_token);
if ($payload) {
  $userid = $payload['sub'];
  // If request specified a G Suite domain:
  //$domain = $payload['hd'];
} else {
  // Invalid ID token
}

You can find more info on this Google Docs Page