Flutter iOS集成Firebase Apple登录:有效令牌仍返回invalid-credential
Flutter + Firebase Auth iOS 项目 Apple 登录异常排查
错误提示:
firebase_auth/invalid-credential — Invalid OAuth response from apple.com
环境信息
firebase_core: 4.6.0firebase_auth: 6.3.0sign_in_with_apple: 7.0.1- Flutter 最新稳定版
- iOS 部署目标:16.0
- 测试环境:TestFlight 分发的物理设备(非模拟器)
Flutter 核心代码(auth_service.dart)
final rawNonce = _generateNonce(); final nonce = _sha256ofString(rawNonce); final appleCredential = await SignInWithApple.getAppleIDCredential( scopes: [AppleIDAuthorizationScopes.email, AppleIDAuthorizationScopes.fullName], nonce: nonce, ); final oauthCredential = OAuthProvider('apple.com').credential( idToken: appleCredential.identityToken, rawNonce: rawNonce, ); return await _auth.signInWithCredential(oauthCredential);
随机数(Nonce)生成逻辑
String _generateNonce([int length = 32]) { const charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._'; final random = Random.secure(); return List.generate(length, (_) => charset[random.nextInt(charset.length)]) .join(); } String _sha256ofString(String input) { final bytes = utf8.encode(input); final digest = sha256.convert(bytes); return digest.toString(); }
设备端令牌解码验证
已确认Apple返回的令牌声明完全符合要求:
iss:https://appleid.apple.comaud: 与App Bundle ID完全匹配nonce: 存在且已正确进行SHA-256哈希处理exp: 令牌未过期email_verified: truenonce_supported: true
Apple 开发者后台配置确认
所有配置项均已验证正确:
- App ID 已启用「Sign in with Apple」并设为主App ID
- Services ID 已配置正确的Firebase回调URL:
https://<project>.firebaseapp.com/__/auth/handler - 已注册带有SIWA功能的密钥,并关联正确的主App ID
- 团队ID已在Apple Developer→Membership中确认无误
已尝试的排查措施
- 从头删除并重新添加Firebase Apple登录提供商
- 多次重新粘贴.p8密钥(尝试不同粘贴方式)
- 生成全新密钥并更新至Firebase,问题依旧
- 在Firebase的Services ID字段尝试替换为Bundle ID,无变化
- 查看GCP审计日志中
identitytoolkit条目,无结果,无法获取服务器端拒绝的具体原因
问题总结
项目中邮箱/密码登录功能正常,Apple侧已确认令牌有效,但Firebase Auth服务器端拒绝该令牌并返回上述错误。
请问是否有人遇到过类似情况?是否存在Firebase控制台不可见的项目级配置错误?
内容的提问来源于stack exchange,提问作者Megaphone




