How to get data form Google_Service_PeopleService?
Asked Answered
P

1

8

I'm currently working with Google_Client api and want to fetch User Name, Phone, Email and User address.

I set-up these scopes:

'https://www.googleapis.com/auth/plus.login',
'https://www.googleapis.com/auth/user.birthday.read',
'https://www.googleapis.com/auth/user.addresses.read',
'https://www.googleapis.com/auth/user.emails.read',
'https://www.googleapis.com/auth/user.phonenumbers.read'

And when I click on the login with google it asks the correct permissions, and then I fetch the access token with the code provided by Google.

After getting the token I request for people_service and profile data like this:

$token = $this->client->fetchAccessTokenWithAuthCode($_GET['code']);
$people_service = new \Google_Service_PeopleService($this->client);
$profile = $people_service->people->get(
             'people/me', 
             array('personFields' => 'addresses,birthdays,emailAddresses,phoneNumbers')
            );

It returns a Google_Service_PeopleService_Person object.

But when I try to use method on it like getPhoneNumbers() it returns a Call to undefined method Google_Service_PeopleService_Person::getNames() error.

What is the problem and what can I do?

Planchette answered 18/9, 2018 at 6:45 Comment(0)
N
5

You do not show how exactly are you setting the scope, and the error might be related to that.

Doing this, I get the correct results:

$scopes = [
  Google_Service_PeopleService::USER_ADDRESSES_READ,
  Google_Service_PeopleService::USER_BIRTHDAY_READ,
  Google_Service_PeopleService::PLUS_LOGIN,
  Google_Service_PeopleService::USER_EMAILS_READ,
  Google_Service_PeopleService::USER_PHONENUMBERS_READ,
];

$client = new Google_Client();
$client->setApplicationName('People API PHP Quickstart');
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');

 // set the scope
$client->setScopes($scopes);

/*  ... actual authentication.   */

$service = new Google_Service_PeopleService( $client );

$optParams = [
    'personFields' => 'names,emailAddresses,addresses,phoneNumbers',
];

$me = $service->people->get( 'people/me', $optParams );

print_r( $me->getNames() );
print_r( $me->getEmailAddresses() );
print_r( $me->getBirthdays() );
print_r( $me->getPhoneNumbers() );
Nutritionist answered 25/9, 2018 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.