Mehrdad Shokri
Mehrdad Shokri

Reputation: 2134

Authenticate google token id with Socialite

I've received token-id from client android app which is generated by google.
How do I authorize the user with socialite?
I'm using google_api client now but it hasn't the convenience of Socialte.

Upvotes: 5

Views: 7038

Answers (3)

Refaat
Refaat

Reputation: 31

Using PHP Google API Client Library

First install library via composer

$ composer require google/apiclient

$client = new Google_Client(['client_id' => env('Google_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
}

for more details check that

https://developers.google.com/identity/sign-in/android/backend-auth?authuser=2

Upvotes: 3

Steven M
Steven M

Reputation: 594

If you can obtain a server auth code then you can exchange it for an accessToken and use it to query the Google+ API.

In my case, I am using a cordova library

https://github.com/EddyVerbruggen/cordova-plugin-googleplus

This will give you (if configured for 'offline') a serverAuthCode

You can send that up to your server. Then, to convert it to an accessToken you can call this socialite method:

$driver = Socialite::driver($provider); // provider is 'google' here
$access_token = $driver->getAccessToken($serverAuthCode);

Then you can get the user info using this token:

$socialUser = $driver->userFromToken($access_token);

Just wanted to help anyone else who was struggling with this.

Upvotes: 3

Raymond Cheng
Raymond Cheng

Reputation: 2505

if you want get userinfo you must use accessToken (sometimes need openid) to get it,recently i made a laravel package to fullfill this request, check here :

https://github.com/Ray-Cheng/laravel-socialite-api

Upvotes: 0

Related Questions