如何在Google Login API中获取用户手机号码?
如何通过Google OAuth2获取用户手机号码及联系人信息?
你目前已经实现了通过Google OAuth2获取用户基本信息,但想要扩展获取手机号码和联系人信息,虽然添加了对应权限但返回结果没变化,我来帮你梳理下问题和解决方法:
你的现有代码及返回结果
代码片段
$client = new Google_Client(); $client->setClientId($client_id); $client->setClientSecret($client_secret); $client->setRedirectUri($redirect_uri); $client->addScope("email"); $client->addScope("profile"); $service = new Google_Service_Oauth2($client); $user = $service->userinfo->get(); //获取用户信息 echo "<pre>"; print_r($user); die;
返回结果
/* Google_Service_Oauth2_Userinfoplus Object ( [internal_gapi_mappings:protected] => Array ( [familyName] => family_name [givenName] => given_name [verifiedEmail] => verified_email ) [email] => MyEmailName@gmail.com [familyName] => Martinish [gender] => [givenName] => Martin [hd] => [id] => 11150231528512531xxx [link] => https://plus.google.com/111502315285125311xxx [locale] => en [name] => Martin Martinish [picture] => https://lh5.googleusercontent.com/-fVHMRUGw5z4/xxx/xxx/IhHnvxaOyAw/photo.jpg [verifiedEmail] => 1 [modelData:protected] => Array ( [verified_email] => 1 [given_name] => Martin [family_name] => Martinish ) [processed:protected] => Array ( ) )
问题分析
你添加的权限是对的,但**Google_Service_Oauth2的userinfo->get()接口本身就不返回手机号码或联系人数据**,这个接口只能获取基础的用户个人资料(姓名、邮箱、头像等)。要拿到手机号和联系人,得用Google的People API。
解决方法
一、获取用户自身的手机号码
- 先去Google Cloud Console里给你的项目启用People API
- 修改代码,使用People API来获取手机号:
// 先添加所需权限 $client->addScope("email"); $client->addScope("profile"); $client->addScope("https://www.googleapis.com/auth/user.phonenumbers.read"); // 初始化People服务 $peopleService = new Google_Service_People($client); // 指定要获取的字段为手机号,调用接口 $userProfile = $peopleService->people->get('people/me', [ 'personFields' => 'phoneNumbers' ]); // 提取并输出手机号码 $phoneNumbers = $userProfile->getPhoneNumbers(); if (!empty($phoneNumbers)) { foreach ($phoneNumbers as $phoneItem) { echo "手机号:" . $phoneItem->getValue() . ",类型:" . $phoneItem->getType() . "<br>"; } } else { echo "未查询到用户的手机号码"; }
二、获取用户的联系人信息(含联系人手机号)
同样依赖People API,步骤如下:
- 确保项目启用People API,并添加
contacts.readonly或contacts权限(只读权限足够的话优先用readonly) - 代码示例:
// 添加联系人读取权限 $client->addScope("https://www.googleapis.com/auth/contacts.readonly"); $peopleService = new Google_Service_People($client); // 获取联系人列表,指定要返回的字段(姓名、手机号) $contactsResponse = $peopleService->people_connections->listPeopleConnections('people/me', [ 'personFields' => 'names,phoneNumbers', 'pageSize' => 50 // 单次请求最多返回50条,可根据需求调整 ]); // 遍历输出联系人信息 foreach ($contactsResponse->getConnections() as $contact) { // 获取联系人姓名 $contactNames = $contact->getNames(); $displayName = !empty($contactNames) ? $contactNames[0]->getDisplayName() : "未命名"; // 获取联系人手机号 $contactPhones = $contact->getPhoneNumbers(); $phoneList = []; if (!empty($contactPhones)) { foreach ($contactPhones as $phone) { $phoneList[] = $phone->getValue() . "(" . $phone->getType() . ")"; } } echo "联系人:{$displayName},手机号:" . implode(", ", $phoneList) . "<br>"; }
三、关键注意事项
- 重新授权:你已经添加了新权限,但用户之前的授权令牌没有包含这些权限,需要让用户重新授权。可以在代码里添加
$client->setApprovalPrompt('force');强制用户重新授权,或者让用户登出后再次登录。 - 字段必须明确指定:People API要求必须通过
personFields参数指定要返回的字段,否则不会返回任何数据,这是很多人容易忽略的点。 - API启用检查:一定要确认在Google Cloud Console里已经启用了People API,否则调用接口会返回权限错误或资源不存在的提示。
内容的提问来源于stack exchange,提问作者Martin AJ
相关产品推荐
相关产品推荐

