В настоящее время я работаю с 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()
ошибка.
В чем проблема и что я могу сделать?
Вы не показываете, как именно вы устанавливаете область, и ошибка может быть связана с этим.
Делая это, я получаю правильные результаты:
$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() );
Других решений пока нет …