phpgoogle-apigoogle-api-php-clientgoogle-domain-api

How to get basic profile information like name and email of users of Google Apps for Work services


I am trying to get basic profile information like name and email of users of Google Apps for Work using PHP client library . According to this question I can do that simply using people->get function with https://www.googleapis.com/auth/plus.login scope.

I tried this and this works fine with @gmail.com accounts, but not with Google Apps for Work users.I also tried using plus_domains service but same results. I managed to get email address using gmail scope but still no luck with getting user's name.

I would like to also mention that users of Google Apps for Work might not have Google plus service activated by admin or they could be using legacy free edition, where Google plus service is not available.


Solution

  • I've got it working using Google Apps for Education, without using the Google Plus API, just the OAuth2 API. These are the scopes I've added:

    $client->addScope(Google_Service_Oauth2::USERINFO_PROFILE);
    $client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
    

    Then you can use it this way:

    $oauthService = new Google_Service_Oauth2($client);
    $userInfo = $oauthService->userinfo_v2_me->get();
    echo "User info:<br>Name: ".$userInfo->name
      ."<br>givenName: ".$userInfo->givenName
      ."<br>familyName: ".$userInfo->familyName
      ."<br>email: ".$userInfo->email;