Как получить данные формы Google_Service_PeopleService? - PullRequest
0 голосов
/ 18 сентября 2018

В настоящее время я работаю с Google_Client API и хочу получить имя пользователя, телефон, электронную почту и адрес пользователя.

Я настраиваю эти области:

'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'

И когда я нажимаю на логин с помощью Google, он запрашивает правильные разрешения, а затем я получаю токен доступа с кодом, предоставленным Google.

После получения токена я запрашиваю people_service и данные профиля:

$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')
            );

Возвращает Google_Service_PeopleService_Person объект.

Но когда я пытаюсьчтобы использовать метод, например getPhoneNumbers(), возвращается ошибка Call to undefined method Google_Service_PeopleService_Person::getNames().

В чем проблема и что я могу сделать?

1 Ответ

0 голосов
/ 25 сентября 2018

Вы не показываете, как именно вы устанавливаете область, и ошибка может быть связана с этим.

При этом я получаю правильные результаты:

$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() );
...