代码之家  ›  专栏  ›  技术社区  ›  Emad Helmi

使用telethon查找会话id并终止会话

  •  1
  • Emad Helmi  · 技术社区  · 6 年前

    在我问这个问题之前我查过了 here all_sessions = client(GetAuthorizationsRequest()).to_dict() 我得到这个结果:

    {
           '_': 'Authorization',
           'api_id': ...,
           'app_name': '...',
           'app_version': '4.1.4',
           'country': 'Unknown',
           'date_active': ...,
           'date_created': ...,
           'device_model': 'SamsungSM-G920F',
           'flags': 0,
           'hash': ...,
           'ip': '...',
           'platform': 'Android',
           'region': '',
           'system_version': 'SDK 23'
    }
    

    session id 上述链接(telethon API文档)中提到。我试着用这些命令:

    client(DestroySessionRequest(api_id))
    client(DestroySessionRequest(hash))
    

    但是不仅没有会话被删除,而且没有来自api的响应,命令在等待响应,没有错误或者没有错误例外。怎么做我能终止会话吗?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Avi_av    6 年前

    要终止其他会话,您需要使用 ResetAuthorizationRequest

    官方文件示例:

    from telethon.sync import TelegramClient
    from telethon import functions, types
    with TelegramClient(name, api_id, api_hash) as client:
        result = client(functions.account.ResetAuthorizationRequest(hash=-12398745604826))
    print(result)
    

    https://lonamiwebs.github.io/Telethon/methods/account/reset_authorization.html#examples

        2
  •  0
  •   Alex    6 年前

    要删除当前会话,请执行以下操作:

    from telethon import TelegramClient
    
    # start session
    client = TelegramClient(username, api_id, api_hash).start()
    
    # Now you can use all client methods listed below, like for example...
    client.send_message('me', 'Hello to myself!')
    
    
    # list all sessions
    print(client.session.list_sessions())
    
    # delete current session (current session is associated with `username` variable)
    client.log_out()
    

    .session 文件来存储每次使用新用户名时的会话详细信息。文件名以username变量开头(例如。 my_username.session 有关电视马拉松的更多信息,请访问 Telethon API documentation .