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

Flutter调用Firebase Functions遇HTTP重定向过多错误(iOS专属)求助

解决iOS端Flutter调用Firebase Functions出现too many HTTP redirects错误

问题背景

两个Flutter项目绑定同一Firebase项目,逻辑完全一致仅业务场景不同。其中一个项目运行正常,另一个在iOS端调用Firebase Callable Functions时抛出firebase_functions/-1007 too many HTTP redirects错误,Android端无异常。已尝试新建项目、重装应用、flutter clean、重新配置Firebase等操作,问题仍存在。

可能的解决方案

1. 核对iOS项目的Firebase配置文件

  • 对比两个项目的GoogleService-Info.plist,确保FUNCTIONS_REGION、FIREBASE_PROJECT_ID等核心字段完全一致。区域配置错误会导致请求被导向错误的函数端点,引发循环重定向。
  • 如果出问题的项目plist中没有FUNCTIONS_REGION字段,手动添加并设置为函数部署的实际区域(比如us-central1)。

2. 显式指定Functions调用区域

在Dart代码中调用函数时,明确指定函数所在区域,避免默认区域不匹配导致的重定向:

Future<dynamic> getIDAuthToken() async {
    try {
      // 替换为你的函数实际部署区域
      HttpsCallable callable = FirebaseFunctions.instanceFor(region: 'us-central1').httpsCallable('createOnfidoUser');
      final response = await callable.call();
      return response.data;
    } on FirebaseFunctionsException catch (e) {
      print(e);
    }
  }

3. 检查iOS ATS配置

iOS的App Transport Security可能限制了重定向行为,可临时调整配置测试:

  • 在Info.plist中添加NSAppTransportSecurity字典,设置NSAllowsArbitraryLoads为YES(仅用于测试,上线需恢复或细化规则)。如果问题解决,再针对Firebase Functions域名添加允许规则,避免全局关闭ATS。

4. 统一Firebase依赖版本

  • 对比两个项目的Podfile.lock,确保FirebaseFunctions等相关Pod的版本完全一致。版本差异可能导致iOS端SDK的请求处理逻辑存在bug。
  • 执行pod update FirebaseFunctions将依赖更新到最新稳定版,或同步为正常项目的版本。

5. 修复Cloud Function的错误处理

当前函数中的catch((err) => {})会吞掉所有错误,无法定位是否是函数内部请求的重定向问题。修改错误处理逻辑,让客户端获取详细错误信息:

exports.createOnfidoUser = functions.https.onCall((request, response) => {
  const onfidoToken = "xxxx";
  const url = "xxxx";
  const options = {
    method: "POST",
    headers: {
      "Authorization": `Token token=${onfidoToken}`,
      "Content-Type": "application/json",
      "Accept": "application/json",
    },
    body: JSON.stringify({
      "first_name": "Jane",
      "last_name": "Doe",
      "dob": "1990-01-31",
      "address": {
        "building_number": "100",
        "street": "Main Street",
        "town": "London",
        "postcode": "SW4 6EH",
        "country": "GBR",
      },
    }),
  };
  return fetch(url, options)
      .then((data) => data.json())
      .catch((err) => {
        // 抛出可被客户端捕获的错误
        throw new functions.https.HttpsError('internal', '请求Onfido失败: ' + err.message);
      });
});

6. 验证Bundle ID配置

确认出问题的iOS项目的Bundle ID已在Firebase控制台的项目设置中正确添加并配置,Bundle ID不匹配可能导致请求被重定向到无效端点。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 01:35:26