7

Hey there,

I have problem with validating user logged in with Google on Android client, Server side is Laravel, In other side when user logged in, it gives me a token that i must verify with Google,

Im using Socialite package, i don't know how i must get user from token, it has an method named getUserFromToken but it occurs many errors,

Which credentials OAuth i must use from google console? Im just using Web Application credentials but no answer!

Code is something like this:

$user = \Laravel\Socialite\Facades\Socialite::driver('google')->userFromToken($googleAuthCode);
    dd($user);

and error is:

Client error: GET https://www.googleapis.com/userinfo/v2/me?prettyPrint=false resulted in a 401 Unauthorized response: { "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 (truncated...)

What i must add to the request?!

Thanks

Katerou22
  • 777
  • 5
  • 19

3 Answers3

0

So, After Searching i found the solution!

Just add code parameter on the request and send Authorization code that given from Android and send to to this method:

$parameters = ['access_type' => 'offline'];

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

Just That! and it gives you the user!

schwertfisch
  • 4,549
  • 1
  • 19
  • 32
Katerou22
  • 777
  • 5
  • 19
  • 2
    Where is the userFromToken here ? I'm creating mobile app with login with google functionality. Mobile app login with google and send the issued token to the laravel API I need to validate it and to login/register the user. So I need this userFromToken to work. – Emil Georgiev Jun 13 '20 at 16:10
0

I've just come across this same problem, and unfortunately I abandoned using Socialite purely for the id token verification from mobile to backend, and switched to using the underlying Google_Client library as per the documentation at https://developers.google.com/identity/sign-in/ios/backend-auth

On your mobile app remember to set the server client id as well as the app client id, and then when you validate the id token on the server, you simply use the server client id

So on the mobile app (I'm using swift here for iOS)

let signInConfig = GIDConfiguration.init(clientID: "iOS Client ID", serverClientID: "Server Client ID")

GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in
                guard error == nil else { return }
                guard let user = user else { return }

                // If sign in succeeded, display the app's main content View.
                user.authentication.do { authentication, error in
                        guard error == nil else { return }
                        guard let authentication = authentication else { return }

                        let idToken = authentication.idToken
                    
                        // Send the idToken to the server here
                    }
              }

Then in Laravel I'm validating as follow, using the same service client id configuration variable as Socialite uses

$client = new \Google_Client(['client_id' => config('services.google.client_id')]);
$payload = $client->verifyIdToken($request->idToken);
Rich2k
  • 23
  • 1
  • 4
0

I have faced exactly same problem. I have tried to solve by both of above solution. But none of them worked for me. Therefore, i have followed https://developers.google.com/identity/sign-in/ios/backend-auth#calling-the-tokeninfo-endpoint

and tried to make a curl request to retrive user data as follows and it works for me:

$token = $request->input('token');
$url = 'https://oauth2.googleapis.com/tokeninfo?id_token=' . $token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
$err = curl_error($ch);  //if you need
curl_close($ch);

$providerUser = json_decode($response);
print_r($providerUser)

i got the output as follows:

stdClass Object
(
  [iss] => https://accounts.google.com
  [azp] => *********-md41p8e2610vmr0t8reb73fkgihvvhu2.apps.googleusercontent.com
  [aud] => *********-d891n2kqophg9qcm03hq6a1cqkuh101r.apps.googleusercontent.com
  [sub] => 102424719522375432743
  [hd] => intelli.global
  [email] => MY_EMAIL
  [email_verified] => true
  [name] => NAME
  [picture] => IMAGE_URL
  [given_name] => FIRST_NAME
  [family_name] => LAST_NAME
  [locale] => en
  [iat] => 1654335656
  [exp] => 1654339256
  [alg] => RS256
  [kid] => 38f3883468fc659abb4475f36313d22585c2d7ca
  [typ] => JWT
)
Bozlur Rahman
  • 507
  • 4
  • 11