Telegram QR-APIPython

Python-Programme
Guest
 Telegram QR-API

Post by Guest »

Es gibt ein Problem, das ich seit mehreren Stunden nicht lösen konnte

Code: Select all

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())
Der Code generiert einen QR-Code und wartet darauf, dass er gescannt wird, damit sich der Benutzer anmelden kann, sodass unser Client im Namen des Benutzers mit der Telegram-API interagieren kann.

Code: Select all

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 der Dokumentation sehen wir, dass der Client während des Scannens des QR-Codes tatsächlich eine Anfrage (das im QR-Code geschriebene Token) an die Methode auth.acceptLoginToken sendet.
Das einzige Problem ist, dass ich während des Scans die Antwort erhalte

Code: Select all

AUTH_TOKEN_ALREADY_ACCEPTED
was bedeutet, dass jemand das Token bereits an diese Methode gesendet hat. Hat jemand anderes dieses Problem festgestellt und können Sie mir sagen, was ich falsch mache?
https://docs.telethon.dev/en/stable/mod ... l#telethon. client.auth.AuthMethods.qr_login
https://docs.telethon.dev/en/stable/mod ... in.QRLogin
https://core.telegram.org/api/qr-login

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post