运行Python Discord Bot时出现HTTPS Error 401问题的解决求助
Discord机器人LoginFailure错误修复方案
错误信息
Traceback (most recent call last): File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 801, in static_login data = await self.request(Route('GET', '/users/@me')) File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 744, in request raise HTTPException(response, data) discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized The above exception was the direct cause of the following exception: Traceback (most recent call last): File "d:\Python\Projects\disco bot\ppap", line 21, in <module> client.run(os.getenv("K")) File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 828, in run asyncio.run(runner()) File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run return loop.run_until_complete(main) File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 647, in run_until_complete return future.result() File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 817, in runner await self.start(token, reconnect=reconnect) File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 745, in start await self.login(token) File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 580, in login data = await self.http.static_login(token) File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 805, in static_login raise LoginFailure('Improper token has been passed.') from exc discord.errors.LoginFailure: Improper token has been passed. Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001C2A7BA30D0> Traceback (most recent call last): File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__ self.close() File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close self._loop.call_soon(self._call_connection_lost, None) File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 751, in call_soon self._check_closed() File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 515, in _check_closed raise RuntimeError('Event loop is closed') RuntimeError: Event loop is closed
机器人代码
import discord import os from dotenv import load_dotenv load_dotenv() intents = discord.Intents.default() intents.typing = False intents.presences = False client = discord.Client(intents=intents) @client.event async def on_ready(): print('We have logged in as {0.user}'.format(client)) @client.event async def on_message(message): if message.author == client.user: return if message.content.startswith('$hello'): await message.channel.send('Hello!') client.run(os.getenv("K"))
修复步骤
核心问题是Token未正确加载或验证失败,以下是针对性排查方案:
检查
.env文件的位置和格式- 确保
.env文件和你的Python脚本(ppap)在同一目录下 .env文件内格式必须为K=你的机器人Token,不要添加引号或多余空格,示例:K=ODkxNzE4Mz...(完整Token字符串)
- 确保
验证Token是否被正确读取
- 在代码中添加调试输出,确认
os.getenv("K")是否获取到有效内容:load_dotenv() token = os.getenv("K") print("Loaded token:", token) # 添加此行调试 - 如果输出为
None,说明.env未被正确加载,可指定文件路径:load_dotenv(dotenv_path="完整的.env文件路径")
- 在代码中添加调试输出,确认
确认Token本身的正确性
- 复制Token时避免携带多余空格或换行符,建议直接从Discord开发者后台复制粘贴到
.env - 确保复制的是机器人专属Token,而非OAuth2的客户端ID或密钥
- 复制Token时避免携带多余空格或换行符,建议直接从Discord开发者后台复制粘贴到
检查依赖与环境兼容性
- 运行
pip show discord.py查看版本,若版本过旧,执行pip install --upgrade discord.py更新到稳定版 - 确认Python 3.9与当前
discord.py版本兼容
- 运行
关于事件循环错误
RuntimeError: Event loop is closed是登录失败导致的衍生问题,解决Token问题后该错误会自动消失,无需单独处理
内容的提问来源于stack exchange,提问作者turtlesXD
相关产品推荐
相关产品推荐

