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

React Native生产环境构建后Agora报错-7,请求解决方案

问题

我正在使用Agora开发一款1v1聊天应用,在React Native的开发环境(development-client)中,可成功实现用户间的音视频通信。但将应用编译为生产或预览版本后,Agora返回错误码-7。

我为房间使用了Token,且与生成访问Token的服务器采用SSL连接。是否遗漏了配置?该如何解决此问题?

我尝试过多种代码修改,包括优化Token处理、在Agora控制台将配置从“Testing”切换为“Live”,但均未消除错误。

以下是我的引擎初始化函数:

const initEngine = async () => {
  if (!_engine.current) {
    _engine.current = createAgoraRtcEngine();
    _engine.current.initialize({
      appId: process.env.EXPO_PUBLIC_AGORA_APP_ID,
    });
    console.log("Engine initialized successfully");
  }
  _engine.current.setChannelProfile(ChannelProfileType.ChannelProfileLiveBroadcasting);
  _engine.current.setClientRole(ClientRoleType.ClientRoleAudience);

  _engine.current.enableVideo();
  _engine.current.startPreview();

  _engine.current.addListener("onLocalAudioStateChanged", (state) => {
    console.log(state);
  });

  _engine.current.addListener("onUserJoined", (connection, uid) => {
    setHostUid(uid);
  });

  _engine.current.addListener("onUserOffline", async (connection, uid) => {
    await endCall();
 });

  _engine.current.addListener("onError", async (err) => {
    await endCall();
  });

  _engine.current.addListener("onJoinChannelSuccess", (connection, uid) => {
    setJoined(true);
  });

  _engine.current.addListener("onConnectionStateChanged", (state, reason) => {
    console.log("Connection state changed:", state, reason);
  });
};
解决方案

Agora错误码-7对应ERR_INVALID_TOKEN,即Token无效。结合开发环境正常、生产环境报错的场景,从以下几个方向排查:

  • 检查Token生成逻辑:生产环境必须使用正式Token生成流程,确保生成Token时的appId、频道名、UID、客户端角色(你设置的是Audience)与代码中的参数完全一致,且Token未过期。测试环境的Token无法在生产环境使用,同时要确认生成Token的服务器使用的是Agora控制台的正式App证书,而非测试证书。
  • 验证环境变量加载情况:生产编译时,EXPO_PUBLIC_AGORA_APP_ID是否被正确注入?可以在生产版本中打印该变量的值,确认与Agora控制台的App ID一致。Expo生产构建时需确保.env配置文件正确,或通过eas build的参数传入正确的环境变量。
  • 确认音视频权限配置:生产环境下,React Native应用需明确配置音视频权限:
    • Android:在AndroidManifest.xml中添加录音、相机权限:
      <uses-permission android:name="android.permission.RECORD_AUDIO" />
      <uses-permission android:name="android.permission.CAMERA" />
      <uses-permission android:name="android.permission.INTERNET" />
      
    • iOS:在Info.plist中添加权限描述:
      <key>NSCameraUsageDescription</key>
      <string>需要使用相机进行视频通话</string>
      <key>NSMicrophoneUsageDescription</key>
      <string>需要使用麦克风进行语音通话</string>
      
  • 检查频道Profile与角色的匹配性:你设置的频道Profile为ChannelProfileLiveBroadcasting,角色是ClientRoleAudience,需确保生成Token时指定的角色与客户端设置一致,否则Token会被判定无效。
  • 排查网络访问限制:生产环境的网络是否存在防火墙、代理或VPN拦截Agora服务?需确保Agora相关域名(如*.agora.io)和端口可正常访问。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 04:57:33