'discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized

This is the huge error im getting:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Roaming\Python\Python310\site-packages\discord\http.py", line 349, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "C:\Users\User\AppData\Roaming\Python\Python310\site-packages\discord\http.py", line 302, in request
    raise HTTPException(r, 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 "f:\Programming\Python\Discord bot\LastOneEverythingCombined\TEST.py", line 112, in <module>
    client.run('OTA0NjMxMzE2MzU0NTg4NzEz.YX-VkQ._B-EnQRE3IhWYl0psJA8UxZuI6s')
  File "C:\Users\User\AppData\Roaming\Python\Python310\site-packages\discord\client.py", line 631, in run
    return future.result()
  File "C:\Users\User\AppData\Roaming\Python\Python310\site-packages\discord\client.py", line 610, in runner
    await self.start(*args, **kwargs)
  File "C:\Users\User\AppData\Roaming\Python\Python310\site-packages\discord\client.py", line 573, in start
    await self.login(*args)
  File "C:\Users\User\AppData\Roaming\Python\Python310\site-packages\discord\client.py", line 424, in login
    await self.http.static_login(token.strip())
  File "C:\Users\User\AppData\Roaming\Python\Python310\site-packages\discord\http.py", line 353, 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 0x000001B4F30E1EA0>
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001B4F30E1EA0>
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001B4F30E1EA0>
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 745, in call_soon
    self._check_closed()
  File "C:\Users\User\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

I have no clue how to fix it and have tried everything on the web This is my code: in the spot of Token i have my token but ofcourse lets keep it hidden :)

client = commands.Bot(command_prefix='.')
Code....
client.run('Token')

Last time i put my entire code there and people got mad so i am now just putting the code i think might be necessary for this problem, if you need any other bits of code just reply with i need all the code to solve this error or more code and then the specific bit that you need or think might be in it



Solution 1:[1]

i just hit my head on my keyboard for a bit over this but i think i found the answer; print(Token) and see what it spits out. maybe you've included a pair of apostrophes or braces that shouldn't be in your token string

Solution 2:[2]

I had the same problem and i fix it by writting [1:-1] after the token: TOKEN = os.getenv('DISCORD_TOKEN')[1:-1]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shib
Solution 2 hugo coto