Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 错误:";传递了不正确的令牌“;不和谐_Python_Python 3.x_Discord_Discord.py - Fatal编程技术网

Python 错误:";传递了不正确的令牌“;不和谐

Python 错误:";传递了不正确的令牌“;不和谐,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,开始学习用Python编写一个简单的Discord bot。我的bot的令牌位于.env文件中,但我收到错误discord.errors.LoginFailure:传递了不正确的令牌。 将令牌放入client.run(此处为令牌)对我来说不起作用,就像对某些人那样 我使用的是Discord开发者门户上的Bot页面中的令牌,而不是常规信息页面中的客户端机密 我正在使用VisualStudio代码 bot.py: import os import discord from dotenv impor

开始学习用Python编写一个简单的Discord bot。我的bot的令牌位于.env文件中,但我收到错误
discord.errors.LoginFailure:传递了不正确的令牌。

将令牌放入
client.run(此处为令牌)
对我来说不起作用,就像对某些人那样

我使用的是Discord开发者门户上的Bot页面中的令牌,而不是常规信息页面中的客户端机密

我正在使用VisualStudio代码

bot.py:

import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)
.env:

完整错误输出:

Eds-MacBook-Pro:~ ed$ python3 /users/ed/documents/supremesauce/supremesauce.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 261, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 225, 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 "/users/ed/documents/supremesauce/supremesauce.py", line 15, in <module>
    client.run(TOKEN)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 640, in run
    return future.result()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 621, in runner
    await self.start(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 584, in start
    await self.login(*args, bot=bot)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 442, in login
    await self.http.static_login(token.strip(), bot=bot)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 265, in static_login
    raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
Eds-MacBook-Pro:~ ed$ 
Eds MacBook Pro:~ed$python3/users/ed/documents/supremesauce/supremesauce.py
回溯(最近一次呼叫最后一次):
静态登录中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/discord/http.py”,第261行
data=wait self.request(路由('GET','/users/@me'))
请求中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/discord/http.py”,第225行
引发HTTPException(r,数据)
discord.errors.HTTPException:401未经授权(错误代码:0):401:未经授权
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“/users/ed/documents/supremasauce/supremasauce.py”,第15行,在
client.run(令牌)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/discord/client.py”,第640行,正在运行
返回future.result()
runner中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/discord/client.py”,第621行
等待自我启动(*args,**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py”,第584行,开头
等待self.login(*args,bot=bot)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/discord/client.py”,第442行,登录
等待self.http.static\u登录(token.strip(),bot=bot)
静态登录中的文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/discord/http.py”,第265行
从exc引发登录失败(“已传递不正确的令牌”)
discord.errors.LoginFailure:传递了不正确的令牌。
Eds MacBook Pro:~ed$

谢谢:)

正如一些评论所说,请确保您的代币周围有引号。除此之外,可能值得尝试重新生成一个新的令牌,并从那里开始


我将再次确保您传递的是一个字符串。

正如一些评论所说,请确保您的令牌周围有引号。除此之外,可能值得尝试重新生成一个新的令牌,并从那里开始


我将再次确保您正在传递字符串。

您是否可以尝试在本地打印令牌,以查看它是否实际被传递,是否仍然存在。是的,我尝试了它实际被传递。我已经提到了那个线程,但我已经做了所有的事情,他们说的话都没有解决我的问题。你的令牌应该是这样的:
MjM4NDk0NzU2NTIxMzc3Nzky.CunGFQ.wUILz7z6HoJzVeq6pyHPmVgQgV4
。如果是这种情况,请确保它是一个字符串,因此被引号包围。您是否可以尝试在本地打印令牌,以查看它是否实际被传递,是否仍然存在。是的,我尝试过它实际被传递。我已经提到了那个线程,但我已经做了所有的事情,他们说的话都没有解决我的问题。你的令牌应该是这样的:
MjM4NDk0NzU2NTIxMzc3Nzky.CunGFQ.wUILz7z6HoJzVeq6pyHPmVgQgV4
。如果是这种情况,请确保它是一个字符串,因此被引号包围。
Eds-MacBook-Pro:~ ed$ python3 /users/ed/documents/supremesauce/supremesauce.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 261, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 225, 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 "/users/ed/documents/supremesauce/supremesauce.py", line 15, in <module>
    client.run(TOKEN)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 640, in run
    return future.result()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 621, in runner
    await self.start(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 584, in start
    await self.login(*args, bot=bot)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/client.py", line 442, in login
    await self.http.static_login(token.strip(), bot=bot)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/discord/http.py", line 265, in static_login
    raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
Eds-MacBook-Pro:~ ed$