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

Android端Firebase Apple登录遇初始状态缺失问题求助

Android端Firebase集成Apple登录报错:缺少初始状态(sessionStorage异常)

用户在Android端通过Flutter集成Firebase Apple登录时触发以下错误:

无法处理请求,原因是缺少初始状态。这可能是浏览器sessionStorage无法访问或被意外清除导致的。

已尝试的操作:

  • 清除默认浏览器数据
  • 允许第三方权限
    但问题仍未解决。

相关代码

Future<void> signInWithApple(SignInMethod flow, {LoginMethod? loginMethod}) async {
    this.loginMethod = loginMethod ?? authProvider.state.loginMethod;
    if (kDebugMode) {
      print('loginMethod: ${this.loginMethod}');
    }
    showLoader();
    try {
      // 为Android配置webAuthenticationOptions
      final authOptions = WebAuthenticationOptions(
        clientId: 'com.dev.applesignin', // 替换为你的客户端ID
        redirectUri: Uri.parse('https://app.firebaseapp.com/__/auth/handler'),
        // 替换为你的重定向URI
      );
      // 启动登录流程
      final appleCredential = await SignInWithApple.getAppleIDCredential(
        scopes: [AppleIDAuthorizationScopes.email, AppleIDAuthorizationScopes.fullName],
        webAuthenticationOptions: authOptions,
      );
      final oauthCredential = OAuthProvider('apple.com').credential(
        idToken: appleCredential.identityToken,
        accessToken: appleCredential.authorizationCode,
      );
      await signInToFirebase(
        oauthCredential,
      );
      hideLoader();
    } catch (e) {
      hideLoader();
      // 处理错误
      print("Apple认证过程中发生错误: $e");
    }
  }

控制台日志

D/SystemUtils(23579): [isSystemApp] begin android.app.Application@74134b4
D/ScreenUtils(23579): isMultiWindow:0,userId:0
D/ScreenUtils(23579): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(23579): isLittleVExist:0,userId:0
D/SystemUtils(23579): userId:0
D/SystemUtils(23579): [isSystemApp] begin android.app.Application@74134b4
D/ScreenUtils(23579): isMultiWindow:0,userId:0
D/ScreenUtils(23579): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(23579): isLittleVExist:0,userId:0
D/SystemUtils(23579): userId:0
[GoRouter] getting location for name: "createWallet"
[GoRouter] pushing /createWallet
I/flutter (23579): unhandled element <filter/>; Picture key: Svg loader
D/SystemUtils(23579): [isSystemApp] begin android.app.Application@74134b4
D/ScreenUtils(23579): isMultiWindow:0,userId:0
D/ScreenUtils(23579): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(23579): isLittleVExist:0,userId:0
D/SystemUtils(23579): userId:0
D/SystemUtils(23579): [isSystemApp] begin android.app.Application@74134b4
D/ScreenUtils(23579): isMultiWindow:0,userId:0
D/ScreenUtils(23579): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(23579): isLittleVExist:0,userId:0
D/SystemUtils(23579): userId:0
D/SystemUtils(23579): [isSystemApp] begin android.app.Application@74134b4
D/ScreenUtils(23579): isMultiWindow:0,userId:0
D/ScreenUtils(23579): hasVivoFreeformTasks:0,userId:0
D/ScreenUtils(23579): isLittleVExist:0,userId:0
D/SystemUtils(23579): userId:0
I/flutter (23579): loginMethod: LoginMethod.signUp
D/SessionLifecycleClient(23579): Sending lifecycle 2 to service
D/SessionLifecycleService(23579): Activity backgrounding at 141606119
I/FA      (23579): Application backgrounded at: timestamp_millis: 1719838599560
I/TRuntime.CctTransportBackend(23579): Making request to: https://firebaselogging-pa.googleapis.com/v1/firelog/legacy/batchlog
D/TrafficStats(23579): tagSocket(233) with statsTag=0xffffffff, statsUid=-1
I/TRuntime.CctTransportBackend(23579): Status Code: 200

解决建议

  1. 核对重定向URI配置
    确保Firebase控制台、Apple Developer后台的Apple登录重定向URI,与代码中的redirectUri完全一致,包括协议、域名和路径。同时在AndroidManifest.xml中添加对应intent-filter,允许应用处理该重定向链接。

  2. 完善权限配置

    • 确认AndroidManifest.xml已添加网络权限:
      <uses-permission android:name="android.permission.INTERNET" />
      
    • Android 11及以上版本,需添加浏览器查询权限:
      <queries>
          <intent>
              <action android:name="android.intent.action.VIEW" />
              <data android:scheme="https" />
          </intent>
      </queries>
      
  3. 切换自定义重定向URI
    避免使用Firebase默认的handler URI,改用应用专属Scheme作为重定向地址,例如:

    final authOptions = WebAuthenticationOptions(
      clientId: 'com.dev.applesignin',
      redirectUri: Uri.parse('your-app-scheme://apple-callback'),
    );
    

    同步在Firebase和Apple后台配置该自定义URI。

  4. 更新插件版本
    确保sign_in_with_apple插件使用最新稳定版,旧版本可能存在Android端sessionStorage兼容问题。

  5. 更换测试浏览器
    部分厂商定制浏览器对sessionStorage有特殊限制,可切换至Chrome等标准浏览器测试。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 13:55:56