简体   繁体   中英

Authorising native app user with Google ID token and Laravel Socialite gives 401

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM