Telethon 无法通过两步验证登录帐户

Telethon Cannot sign into accounts with two step verfication

我正在尝试使用带有两步验证号码的 telethon 登录电报。我用这个代码,

client = TelegramClient(f'sessions/1', API_ID, API_HASH)
client.connect()
phone = input('phone ; ')
y = client.send_code_request(phone)
x = client.sign_in(phone=phone, password=input('password : '), code=input('code :'))

但是它仍然说该帐户是两步保护的。 如果没有这种方法,有没有更简单的方法来做到这一点,或者...我怎样才能正确使用这种方法?

我想完全通过代码登录帐户,而无需在终端中输入任何内容(这里我使用输入只是为了测试。稍后我将连接一个 GUI,用户可以在其中输入详细信息)所以我不认为client.start() 会起作用。在将参数传递给 client.start() 方法时我有点困惑。

任何帮助将不胜感激。 谢谢。

您还需要传递从 client.send_code_request(phone) 返回的 phone_code_hash

你可以试试(见sign_in with phone_code_hash and send_code_request的函数调用):

y = client.send_code_request(phone)
client.sign_in(phone=phone, password=input('password : '), code=input('code :'), phone_code_hash=y.phone_code_hash)

这就是我在代码中实现它的方式,使用了 client.start().

实现中的位
phone = input("Enter phone: ")
await client.send_code_request(phone, force_sms=False)
value = input("Enter login code: ")
try:
    me = await client.sign_in(phone, code=value)
except telethon.errors.SessionPasswordNeededError:
    password = input("Enter password: ")
    me = await client.sign_in(password=password)