You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

2022年如何通过Sign-in with Google API获取性别、生日及城市信息?

关于Sign-in with Google API获取额外用户信息的问题解答

我已完成经谷歌批准且可正常运行的Sign-in with Google API集成,希望除基础信息外,额外获取用户的gender(性别)、birthday(生日)及city(城市)信息。当前用户登录时谷歌返回的OAuth响应示例如下:

Google\Service\Oauth2\Userinfo Object ( [internal_gapi_mappings:protected] => Array ( [familyName] => family_name [givenName] => given_name [verifiedEmail] => verified_email ) [email] => contact@xxxxxxxxxxx.com [familyName] => xxxxxxxx [gender] => [givenName] => xxxxxx [hd] => xxxxxxxxxxx.com [id] => xxxxxxxxxxxxxxxxxxxxxxxx [link] => https://plus.google.com/xxxxxxxxxxxxxxxxxxxxxx [locale] => en [name] => xxxxxxx xxxxxxxxx [picture] => https://lh3.googleusercontent.com/a/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx [verifiedEmail] => 1 [modelData:protected] => Array ( [verified_email] => 1 [given_name] => xxxxxx [family_name] => xxxxxxxxxx ) [processed:protected] => Array ( ) )

*注:该用户谷歌账户中性别已设为公开,但返回的gender字段仍为空。


问题与解答

1. 2022年是否仍可实现该需求?

可以实现,但部分字段存在获取限制:

  • 性别:仅能获取用户设置为「公开」的性别信息,谷歌虽缩减了该字段的默认返回场景,但仍支持通过特定API获取。
  • 生日:仅能获取用户主动授权且设置为可分享的生日信息,需用户明确同意授权。
  • 城市:无法直接通过Sign-in with Google API获取,需借助Google People API读取用户住址信息(若用户授权且信息存在)。

2. 是否可通过Sign-in with Google API或其他API(含多请求方式)实现?

  • Sign-in with Google API(OAuth2 Userinfo端点):仅返回基础信息,无法获取生日、城市,性别字段也可能因谷歌政策限制返回空值(即使用户设为公开)。
  • Google People API:这是获取额外用户信息的核心方案,支持获取性别、生日、住址(含城市)等信息,需在用户登录后额外调用该API接口。

3. 在Google Cloud Console的OAuth配置中需设置哪些权限范围?

需添加以下OAuth权限范围(按需选择,避免过度申请):

  • https://www.googleapis.com/auth/userinfo.profile:基础用户信息(含性别,但可能返回空)
  • https://www.googleapis.com/auth/user.birthday.read:读取用户生日信息(需用户授权)
  • https://www.googleapis.com/auth/user.addresses.read:读取用户住址信息(含城市,需用户授权)

注意:所有权限范围需经过谷歌审核,需在OAuth consent screen中明确说明申请权限的用途。

4. 是否有可用的PHP示例或相关文档?

PHP示例代码片段(基于Google Client Library):

// 初始化已授权的Google Client实例(假设已完成登录流程)
$client = new Google\Client();
$client->setAccessToken($_SESSION['access_token']);

// 调用Google People API获取额外信息
$peopleService = new Google\Service\PeopleService($client);
$profile = $peopleService->people->get('people/me', [
    'personFields' => 'genders,birthdays,addresses'
]);

// 提取性别
if (!empty($profile->getGenders())) {
    $gender = $profile->getGenders()[0]->getValue();
}

// 提取生日
if (!empty($profile->getBirthdays())) {
    $birthday = $profile->getBirthdays()[0]->getDate();
    $year = $birthday->getYear();
    $month = $birthday->getMonth();
    $day = $birthday->getDay();
}

// 提取城市
if (!empty($profile->getAddresses())) {
    $city = $profile->getAddresses()[0]->getCity();
}

最佳实现方案

  1. 权限申请:在Google Cloud Console配置OAuth consent screen,添加所需的People API权限范围,提交审核确保权限获批。
  2. 登录流程优化:用户登录时,除基础授权外,引导用户同意额外的生日、住址读取权限。
  3. 多API调用:完成OAuth登录获取access_token后,调用Google People API的people.get接口,指定需要的字段(genders、birthdays、addresses)获取目标信息。
  4. 异常处理:针对用户未授权、信息未设置、API返回空值等情况,做好容错处理,避免业务逻辑中断。

内容的提问来源于stack exchange,提问作者Luca Pennisi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 23:25:18