E2B沙箱出现403权限错误的原因排查与解决求助
E2B沙箱出现403权限错误的原因排查与解决求助
大家好,我使用E2B沙箱已经有2个月左右了,之前因为一些原因停了几周没碰相关代码,现在回来测试之前的项目时,遇到了403权限错误,具体报错信息如下:
File "D:\project_path\Lib\site-packages\e2b\sandbox_async\sandbox_api.py", line 292, in _create_sandbox
raise handle_api_exception(res)
e2b.exceptions.SandboxException: 403: b'\n\n \n403 Forbidden \n \n \nError: Forbidden\nYour client does not have permission to get URL
\n \n \n'/sandboxesfrom this server.
下面是我代码的相关片段,供大家参考:
沙箱管理类代码
import logging from typing import Optional, Literal from e2b import AsyncSandbox class AgentSystemSandbox: def __new__(cls, api_key: str, template: str, timeout: Optional[int] = None): if not hasattr(cls, "instance"): cls.instance = super(AgentSystemSandbox, cls).__new__(cls,) return cls.instance def __init__(self, api_key: str, template: str, timeout: Optional[int] = None): self._api_key = api_key self._template = template self._timeout = None self.set_timeout(timeout) self._sandbox = None async def create_sandbox( self, timeout: Optional[int] = None, if_exists: Literal["reuse", "create"] = "reuse", ) -> AsyncSandbox: if not timeout: timeout = self._timeout if self._timeout is not None else 600 if self._api_key is None: raise ValueError("API key must be set before creating a sandbox") if self._template is None: raise ValueError("Template must be set before creating a sandbox") if self._sandbox is None: self._sandbox = await AsyncSandbox.create( template=self._template, timeout=timeout, api_key=self._api_key ) elif if_exists == "create": self._sandbox = await AsyncSandbox.create( template=self._template, timeout=timeout, api_key=self._api_key ) return self._sandbox
环境变量与初始化代码
import os import logging from dotenv import load_dotenv logger = logging.getLogger(name) load_dotenv() class EnvVarNotSet(AgentError): def __init__(self, msg: str): super().__init__("AgentSystemRuntime", msg, "ENV_VAR_NOT_SET") class AgentSystemRuntime: def __init__(self): self.E2B_API_KEY = os.environ.get("E2B_API_KEY", "") if not self.E2B_API_KEY: logger.error("E2B_API_KEY env var not set") raise EnvVarNotSet("E2B_API_KEY env var not set") print(f"E2b api key: {self.E2B_API_KEY}") self.sandbox_creator = AgentSystemSandbox(self.E2B_API_KEY, "actual-template-name", 1800)
注:代码中的actual-template-name是占位符,我实际使用的是正确的模板名称。
我已经做了这些排查工作:
- 确认API密钥正确,甚至重新生成了新的密钥,问题依旧
- 因为所在地区无法直接访问E2B,我使用了V2RayN(Windows x64)VPN,并且开启了Tun模式,确保所有应用(包括终端)的流量都走VPN——我测试过用curl访问其他被屏蔽的网址是正常的
- 之前遇到过类似问题,当时开启Tun模式的VPN就能解决,但这次换了多个VPN节点都没用
想请教大家,这个403错误到底是什么原因导致的?有没有可行的解决办法?
内容来源于stack exchange
相关产品推荐
相关产品推荐

