I'm trying to implement Google Apps SSO using OAuth 2. I'm using the Google PHP Client Library for this purpose. I set the scope to 'email' and 'profile' during the authentication:
$this->client->setScopes('email', 'profile');
I'm able to get the email address with verifyIdToken, and the first name and the last name using Google Plus.
if ($this->client->getAccessToken()) {
$tokenData = $this->client->verifyIdToken()->getAttributes();
// Email: $tokenData['payload']['email']
$plus = new \Google_Service_Plus($this->client);
$me = $plus->people->get('me');
// First name: $me['modelData']['name']['givenName']
// Last name: $me['modelData']['name']['familyName']
}
The problem I'm facing is that many users use Google Apps without having a profile on Google+. My question is: How do I get the first and last name of the authenticated user without using Google Plus Service?
Note: I've tried posting to https://www.googleapis.com/oauth2/v1/userinfo with the correct access token, but this is soon to be deprecated and moreover, it also returns empty first name and last name for users who do not have Google+ profiles.
I ended up using the OAuth 2.0 Service included in the library.
require_once 'Google/Service/Oauth2.php';
$oAuth2 = new \Google_Service_Oauth2($this->client);
$oAttr = $oAuth2->userinfo->get();
First Name is $oAttr['givenName']
and Last Name is $oAttr['familyName']
I also had to change the scope to "openid email profile":
$this->client->setScopes('openid email profile');