Discord.py: problems with authenticating token

Created on 4 Feb 2020  路  7Comments  路  Source: Rapptz/discord.py

I am unable to run any python code within Visual Studio Code for discord. I use vsc version 1.41.1 on Ubuntu 18.04. the version of python being used is 3.6.9 on the internal terminal; as indicated with the python3. The error message is persistent, and regardless of what method I use to access the file, the error keeps returning. the code is as follows:

```def read_token():
path = '/path/used/to.txt'
with open(path, "r") as f:
lines = f.read()
return lines[0].strip()

token = read_token
print(f'{token}')

try:
bot.run(f'{token}')
except discord.errors.LoginFailure as e:
print('login failed, ERROR 401 unauthorized')
doing this will output this on the terminal: user@fml:~$ /usr/bin/python3 /path/used/to/bot.py

login failed, ERROR 401 unauthorized

The error gives this information :

```Traceback (most recent call last):
  File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/http.py", line 256, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/http.py", line 220, 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 "/home/falwaeth/BotDev/bot.py", line 32, in <module>
    bot.run(f'{token}')
  File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/client.py", line 640, in run
    return future.result()
  File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/client.py", line 621, in runner
    await self.start(*args, **kwargs)
  File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/client.py", line 584, in start
    await self.login(*args, bot=bot)
  File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/client.py", line 442, in login
    await self.http.static_login(token.strip(), bot=bot)
  File "/home/falwaeth/.local/lib/python3.6/site-packages/discord/http.py", line 260, in static_login
    raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.

What I do not understand is the improper token. If I change the bot.run() to have the actual token in it, it will work. The permission of the .txt file is set to be able to be read by everyone. Any ideas?

question

Most helpful comment

lines = f.read()
return lines[0].strip()

Just realised ... given that .read() returns a string, doesn't that mean the second line is only returning the first character of your token? I'm guessing that's your problem.

All 7 comments

Well, that's just one silly mistake here.
As you can see, your token is <function read_token at 0x7f2d0edba268>, and not the actual token string.
What you need to do is simply call your read_token function.

token = read_token()  # in your code it was 'read_token'
print(token)
...
bot.run(token)

This is not an issue with discord.py.
GitHub issues for this repository should be used to report issues with this library.
They are not a medium for requesting help with Python.
For further help specific to using this library, you should join either the official discord.py server or the Discord API server, as the README also links.

Well, that's just one silly mistake here.
As you can see, your token is <function read_token at 0x7f2d0edba268>, and not the actual token string.
What you need to do is simply call your read_token function.

token = read_token()  # in your code it was 'read_token'
print(token)
...
bot.run(token)

it still gives the same error upon editing the code.
I do think this is an error with discord.py ; everything has the proper configs

There is no reason why this would be an issue with discord.py given that you are attempting to pass the same string in both cases. The problem is that you are not obtaining that string you expect to get from your function. That is an issue with your code, not with discord.py.

do you have any ideas on what could be causing the token to have no authentication?

Come to the discord server like the message a couple above mentioned and we can assist with finding out. This isn't the place for it, as Harmon's message mentions

lines = f.read()
return lines[0].strip()

Just realised ... given that .read() returns a string, doesn't that mean the second line is only returning the first character of your token? I'm guessing that's your problem.

Was this page helpful?
0 / 5 - 0 ratings