我目前正在使用Google_Client api,并希望获取用户名,电话,电子邮件和用户地址。
我设置了以下范围:
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
//HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename = " + fileName);
当我单击Google登录时,它询问正确的权限,然后使用Google提供的代码获取访问令牌。
获取令牌后,我请求'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'
和配置文件数据,如下所示:
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()
错误。
问题出在哪里,我该怎么办?
答案 0 :(得分:2)
您没有显示设置范围的精确程度,并且该错误可能与此有关。
这样做,我得到正确的结果:
$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() );