11

I am trying to add a login option to my website for people with Google accounts. I have been able to implement this Facebook but having issues getting user account information with Google.

I am using the Google PHP SDK located here: https://github.com/google/google-api-php-client

$client = new Google_Client();
$client->setClientId($this->ci->config->item('client_id', 'google'));
$client->setClientSecret($this->ci->config->item('client_secret', 'google'));
$client->setRedirectUri($this->ci->config->item('callback_uri', 'google'));
$client->addScope('https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.login');
$this->client->createAuthUrl();

But now how do I access the user's email address and other basic information?

I see in the Google PHP SDK a method called getAccountInfo() in the class Google_Service_IdentityToolkit. However, the parameter it requires is postBody but I am not sure how to get/build that.

spsquared123
  • 123
  • 1
  • 1
  • 7

2 Answers2

17

This will return a Google_Service_Oauth2_Userinfoplus object with the info you're probably looking for:

$oauth2 = new \Google_Service_Oauth2($client);
$userInfo = $oauth2->userinfo->get();
print_r($userInfo);

Where $client is an instance of Google_Client

Outputs:

Google_Service_Oauth2_Userinfoplus Object
(
    [internal_gapi_mappings:protected] => Array
        (
            [familyName] => family_name
            [givenName] => given_name
            [verifiedEmail] => verified_email
        )

    [email] => 
    [familyName] => 
    [gender] => 
    [givenName] => 
    [hd] => 
    [id] => 123456
    [link] => https://plus.google.com/123456
    [locale] => en-GB
    [name] => someguy
    [picture] => https://lh3.googleusercontent.com/-q1Smh9d8d0g/AAAAAAAAAAM/AAAAAAAAAAA/3YaY0XeTIPc/photo.jpg
    [verifiedEmail] => 
    [modelData:protected] => Array
        (
        )

    [processed:protected] => Array
        (
        )

)

Also note that you need to request the https://www.googleapis.com/auth/userinfo.profile scope as well.

antriver
  • 848
  • 1
  • 10
  • 21
  • 2
    Upvote. Do you know how can I get some additional info from user? i.e his phone number, his address etc ..! Should I use any specific library? – Shafizadeh Jan 29 '18 at 14:18
  • 1
    Great solution. One technical tho...The basic sign-in scopes to be set in Google_Client should be just 'email' and / or 'profile' (depending on what info do you need) since some time ago. The 'https://www.googleapis.com/auth/userinfo.profile' one is deprecated, but kept for backwards compatibility, I think. More info on official website https://developers.google.com/identity/protocols/googlescopes#google_sign-in – The Vojtisek Apr 17 '19 at 12:15
  • This API is Deprecated. You have to switch to the Google_Client – Peter Fox May 26 '23 at 09:35
5

You should be able to get this info by constructing a Google_Service_OAuth2 object, passing in the Google_Client as a paramater, then get the user info from there.

$oauth2 = new Google_Service_Oauth2($client);
$userInfo = $oauth2->userinfo;
Pinke Helga
  • 6,378
  • 2
  • 22
  • 42
WoogieNoogie
  • 1,258
  • 1
  • 11
  • 21
  • 1
    The cases seem to have changed since you posted this answer. It's now `Google_Service_Oauth2` (lowercase a) and `userinfo` (lowercase i) – antriver Aug 18 '15 at 02:21
  • 1
    @antriver You literally resolved my entire problem. With Wamp it didn't matter on the case sensitivity, when I moved it to lamp it crashed. So linux is OCD and windows don't care. – Matthew Auld Mar 02 '17 at 23:48