Reputation: 1963
I am trying to modify Google's PHP idtoken example to not only return email address (which is does) but also return first and last name.
I have modified line 36 from:
$client->setScopes('email');
to:
$client->setScopes( array( "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile" ) );
When examining $token_data
is does not contain this information. There was a question previously asked about this, but is uses contrib/Google_Oauth2Service.php
which is no longer included in the official google-api-php-client
so I'd rather not use this approach.
What do I need to change in order for the code to return email address, first name, and last name? Full name would be OK is first/last is not available.
Upvotes: 1
Views: 1501
Reputation: 1963
I was able to get this working with the help of Mahasish Shome's comment. I also had to enable "Google+ API" in the Google Developer Console.
Here is the code I needed to add:
require_once 'Google/Service/Plus.php';
// further on down in the example code...
$token_data = $client->verifyIdToken()->getAttributes(); // in preexisting code
// my new code...
$plus = new Google_Service_Plus( $client );
$me = $plus->people->get('me');
// further on down in the example code...
var_dump($token_data); // in preexisting code
// my new code...
var_dump($me);
Upvotes: 3