Telegram qr api

there’s a problem I haven’t been able to solve for several hours

client = TelegramClient('session_name', api_id, api_hash)

async def generate_qr():
    try:
        await client.start()

        qr_login = await client.qr_login()

        print(qr_login.expires)

        qr_url = qr_login.url

        if qr_url:
            img = qrcode.make(qr_url)
            print(qr_url)
            img.save("login_qr.png")
            print("QR code saved as login_qr.png")

            user = await qr_login.wait()
            print(f"Logged in as: {user.first_name}")

            print(f"Token: {urlsafe_b64encode(qr_login.token).decode('utf-8')}")
        else:
            print("Failed to generate QR code URL.")
        
    except Exception as e:
        print(f"An error occurred: {e}")

client.loop.run_until_complete(generate_qr())

The code generates a QR code and waits for it to be scanned so that the user can log in, allowing our client to interact with the Telegram API on behalf of the user.

Exporting a login token

First of all, auth.exportLoginToken must be called by the app that wants to log in to an existing Telegram account. The method will return an auth.loginToken constructor, containing a binary login token and an expiration date (usually 30 seconds).

The login token must be encoded using base64url, embedded in a tg://login?token=base64encodedtoken URL, and shown in the form of a QR code to the user. After the expiration of the current QR code, the auth.exportLoginToken method must be recalled, and a new QR code must be generated automatically.

Accepting a login token

In order to log in, the QR code must be scanned and accepted by an already logged-in Telegram app using auth.acceptLoginToken. The token must be extracted from the tg://login URI and base64url-decoded before using it in the method.

Possible errors returned by the method are:

400 - AUTH_TOKEN_INVALID: an invalid authorization token was provided
400 - AUTH_TOKEN_EXPIRED: the provided authorization token has expired, and the updated QR code must be re-scanned
400 - AUTH_TOKEN_ALREADY_ACCEPTED: the authorization token was already used
The method will return an authorization object containing info about the app and session that we just authorized.

Confirming (importing) the login token

After the logged-in app calls auth.acceptLoginToken and accepts the login token, the app that is trying to log in will receive an updateLoginToken update, which should trigger a second call to the auth.exportLoginToken method.

This second call should then return an auth.loginTokenSuccess constructor, indicating successful login, essentially allowing further authorized interaction with the API.

If, however, there is a DC mismatch between the two apps, auth.loginTokenMigrateTo is returned instead, to which the app that is trying to log in should respond by calling auth.importLoginToken with the specified token, to the specified DC.

This call should then finally return an auth.loginTokenSuccess constructor.

In the documentation, we see that the client, during the QR code scanning, actually sends a request (the token written in the QR code) to the auth.acceptLoginToken method.

The only problem is that during the scan, I receive the response

AUTH_TOKEN_ALREADY_ACCEPTED

which means someone has already sent the token to this method. Has anyone else encountered this problem and can you tell me what I am doing wrong?

https://docs.telethon.dev/en/stable/modules/client.html#telethon.client.auth.AuthMethods.qr_login
https://docs.telethon.dev/en/stable/modules/custom.html#telethon.tl.custom.qrlogin.QRLogin
https://core.telegram.org/api/qr-login

New contributor

abdul abraam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật