使用Hybridauth 3.9实现Google登录无法获取用户姓名仅能获取邮箱
解决Hybridauth 3.9获取Google用户姓名等信息为空的问题
问题描述
使用Hybridauth 3.9实现Google登录后,仅能获取用户邮箱ID,firstName、lastName等字段均为null。Google Cloud OAuth同意屏幕发布状态为“Testing”。
用户返回的Profile示例:
{"identifier":"*****3478","webSiteURL":null,"profileURL":null,"photoURL":"https:\/\/lh3.googleusercontent.com\/a-\/ACB-R5STKfcxZY0D0fSaRRCvuV0bYbhFLM0bjxxwAERB=s96-c","displayName":null,"description":null,"firstName":null,"lastName":null,"gender":null,"language":null,"age":null,"birthDay":null,"birthMonth":null,"birthYear":null,"email":"myemailid@gmail.com","emailVerified":"myemailid@gmail.com","phone":null,"address":null,"country":null,"region":null,"city":null,"zip":null,"data":[]}
解决方案
1. 修改Google授权Scope配置
当前代码中Google的scope仅设置为email,这是无法获取姓名等信息的核心原因。需要扩展授权范围,在主配置文件和callback.php的$config中同步修改:
原配置:
'scope' => 'email',
修改为包含profile权限的配置:
'scope' => 'email profile',
或者使用更明确的API端点权限:
'scope' => 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile',
修改后,Google会返回用户的姓名、头像等基础个人信息。
2. 确认Testing状态下的测试用户设置
在Google Cloud控制台的OAuth同意屏幕页面,确保你使用的测试账号已被添加到测试用户列表中。处于Testing状态的OAuth应用,仅测试用户能正常授权并返回所有申请的权限数据,非测试用户会被拒绝授权。
3. 优化用户Profile获取逻辑
避免重复调用getUserProfile(),一次获取后即可访问所有属性:
$userProfile = $adapter->getUserProfile(); echo $userProfile->firstName; // 现在可以正常获取姓名 echo $userProfile->email;
内容的提问来源于stack exchange,提问作者Bipul Alam
相关产品推荐
相关产品推荐

