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

Flutter集成LinkedIn登录+Firebase Auth iOS端无法获取用户邮箱问题

问题根因
  • linkedin_login: ^2.0.1插件跨端默认权限配置不一致:安卓端默认会携带r_emailaddress邮箱权限申请,而iOS端默认仅申请r_liteprofile基础资料权限,未包含邮箱权限,导致拿到的授权令牌本身就没有获取用户邮箱的权限,云函数侧自然无法从LinkedIn接口拿到邮箱数据写入Firebase用户资料。
  • 若已显式配置权限仍有问题,大概率是LinkedIn开发者后台的iOS端应用配置未完成关联:没有在对应应用的「Sign In with LinkedIn」产品配置里添加iOS端的Bundle ID白名单,导致权限申请被拦截,无法获取邮箱权限。
  • 云函数生成自定义令牌逻辑存在兼容问题:如果安卓端传过来的令牌可以拿到邮箱,iOS传的拿不到,云函数未做强制写入处理,没有主动把获取到的邮箱显式写入Firebase用户的email字段,最终导致Firebase返回的用户对象邮箱为null。
修复方案
  1. 显式统一跨端登录权限配置
    初始化LinkedInAuthCodeWidget时,强制指定scope参数包含邮箱权限,不要依赖插件默认配置:
LinkedInAuthCodeWidget(
  /// 其余配置参数保持不变
  scope: 'r_liteprofile r_emailaddress',
)
  1. 校验LinkedIn开发者后台配置
    确认你的应用已经开通「Sign In with LinkedIn」产品,且在该产品的设置页中,已经正确添加你iOS应用的Bundle ID,没有拼写错误。
  2. 调整云函数生成自定义令牌的逻辑
    拿到前端传的LinkedIn授权令牌后,先调用LinkedIn邮箱接口拉取用户邮箱,无论安卓iOS都显式调用Firebase Admin SDK的updateUser接口写入邮箱信息后再生成自定义令牌:
// 云函数示例(Node.js环境)
const emailRes = await fetch('https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))', {
  headers: { Authorization: `Bearer ${linkedinToken}` }
})
const emailData = await emailRes.json()
const userEmail = emailData.elements[0]['handle~'].emailAddress
// 先更新用户资料再生成令牌
await admin.auth().updateUser(uid, {
  email: userEmail,
  emailVerified: true,
  displayName: 已获取到的用户显示名
})
const customToken = await admin.auth().createCustomToken(uid)
  1. 前端增加令牌刷新逻辑
    调用signInWithCustomToken成功后,除了调用reload(),额外强制刷新一次ID令牌确保拿到最新用户资料:
await FirebaseAuth.instance.currentUser?.reload();
await FirebaseAuth.instance.currentUser?.getIdToken(true);
final latestUser = FirebaseAuth.instance.currentUser;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 00:06:00