skip to Main Content

I’m trying to log into telegram using telethon with a number with two-step verification. I use this code,

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 :'))

But It still says that the account is two-step protected.
Is there any easier way to do this without this method or… how can I properly use this method?

I want to log into the account fully from the code without typing anything in the terminal (Here I used inputs just for testing. I will connect a GUI later where users can enter the details) so I don’t think client.start() will work. and I’m a little confused when it comes to passing the parameters to client.start() method.

Any help would be really appreciated.
Thank you.

3

Answers


  1. You also need to pass the phone_code_hash returned from client.send_code_request(phone).

    You could try (see the function call of 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)
    
    Login or Signup to reply.
  2. This is how I implemented it in my code, using bits from the implementation of 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)
    
    Login or Signup to reply.
  3. This works like a charm:

    client.start(phone)
    

    Watch documentation if needed.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search