1

I am trying to generate server auth code in android

gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
            .requestServerAuthCode(getString(R.string.server_client_id), false)
            .build();

Then I have tried to get server auth code like this.

result.requestServerAuthCode(getString(R.string.server_client_id), false)

Suppose i got a auth token like 'bla bla bla';

Then using laravel socialite I am trying to get user on the server side

Socialize::driver('google')->userFromToken('bla bla bla')

It shows me error

GuzzleHttp\Exception\ClientException with message 'Client error: GET https://www.googleapis.com/plus/v1/people/me?prettyPrint=false resulted in a 401 Unauthorized` response: {"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","loc (truncated...)

BNK
  • 23,994
  • 8
  • 77
  • 87
Anand Siddharth
  • 967
  • 1
  • 9
  • 30
  • Use that auth code to get the access token, see https://developers.google.com/identity/protocols/OAuth2, then use the access token for that endpoint – BNK Aug 03 '16 at 08:32
  • Thankyou for replying! I was looking on the web for that. can you show me how to get access_token from php or simply curl @BNK – Anand Siddharth Aug 03 '16 at 08:36
  • Sorry I am not familiar with php and curl, however, I think you can find more at https://github.com/google/google-api-php-client and http://stackoverflow.com/questions/28390718/not-able-to-fetch-google-oauth-2-0-access-token – BNK Aug 03 '16 at 08:43
  • got it any ways thanks – Anand Siddharth Aug 03 '16 at 09:23
  • @AnandSiddharth Can you please help me even i am also in the same phase , i am trying to figure it out using laravel socialite plugin – Manoj Rammoorthy Dec 02 '16 at 11:39
  • @AnandSiddharth so do we have to use google client library for that – Manoj Rammoorthy Dec 04 '16 at 19:20
  • @AnandSiddharth You had missed to mention scope and that's why you were receiving the error. i.e. `Socialize::driver('google')->scopes(['profile','email'])->userFromToken('bla bla bla')` – Sushant Pimple Jun 19 '18 at 07:18

2 Answers2

4

Actually the code sent by google on android is not the access token to get access token you can do this on laravel controller.

Install this composer library https://github.com/pulkitjalan/google-apiclient

$client = new \PulkitJalan\Google\Client(['client_id' => 'YOUR_CLIENT_ID', 'client_secret' => 'YOUR_SECRET', 'redirect_uri' => 'YOUR_REDIRECT_URI', 'developer_key' => 'YOUR_KEY']);
$google = $client->getClient();
$google->authenticate($token);
$access_token = $client->getAccessToken()["access_token"];

//and now here you go
$user = Socialize::driver('google')->userFromToken($access_token);
Anand Siddharth
  • 967
  • 1
  • 9
  • 30
2

The easiest way is to use the Google_Client class that is provided from Google. you can find it here

Now, you've got two option:

  1. You can send an id_token to the server, and validate it just as the link said and then just fetch the user info from the object received.
  2. You can send the backend_auth_code to the server instead of the id_token, and then use the method fetchAccessTokenWithAuthCode($code) of the Google_Client class. This gives you the access token, id token and other stuff. You can then use that access_token with Laravel Socialite

    $client = new Google_Client([
        'client_id' => config('services.google.client_id'),
        'client_secret' => config('services.google.client_secret')
    ]);
    
    $data = $client->fetchAccessTokenWithAuthCode($code);
    
    $user = Socialite::driver('google')->scopes(['profile','email'])->userFromToken($data['access_token']);
    
gencblakqori
  • 104
  • 7