微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何从Google_Service_PeopleService获取数据?

我目前正在使用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和profile数据,如下所示:

$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()之类的方法时,它会返回对未定义方法调用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,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() );

原文地址:https://www.jb51.cc/php/134337.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐