Telegram channel- how to get access_hash?
Asked Answered
C

3

6

I try really hard to understand howto use Telegram api with telethon. I have some Channels in Telegram, where i want to delete older Messages. Using inputpeerchannel() i need channel_id (No Problem) and channel_hash. I cant findout howto get this channel_hash by channel_id. Thank you from germany

Contraceptive answered 13/10, 2017 at 19:7 Comment(2)
have you found a solution?Idellaidelle
Can anyone share an updated solution please?Coracoid
S
11

In order to find channel access_hash, you should resolve channel username. Original MTProto method contacts.resolveUsername#f93ccba3 gets @username and returns channel info including access_hash.

In telethon you need to invoke ResolveUsernameRequest to call the above original MTProto method. You may use this code to resolve username to access_hash:

client = TelegramClient(session_file, api_id=00000, api_hash='XXXXX')
client.connect()
response = client.invoke(ResolveUsernameRequest("your_channel_id"))
print(response.chats[0].access_hash)
client.disconnect()
Sequent answered 14/10, 2017 at 5:39 Comment(3)
"your_channel_id" is the username or the channel_id? it does not work for me with the channel_id, it works with the username but I only have the channel_idIdellaidelle
"your_channel_id" is a string and it's the username. You can't access a channel only by channel_id. You need access_hash too.Sequent
Do you know if the access_hash will be expired?Buehrer
C
2

There are 4 ways to get access hash:

  1. From a Group
  2. From username
  3. From contact list
  4. From chats message

So, if you have id only, there is no way to get access hash

Changeful answered 30/4, 2020 at 9:0 Comment(3)
but where is this user_hash?Hudnut
You need to use the method provide by telegram, after that you get an object containing information about ID, Access_Hash, Name,...Changeful
Could you clarify how exactly is possible to get access_hash from a group?Nosewheel
C
0

Here is the updated code. replace appid, apphash and username

with TelegramClient('session.session', <app-id>,<app-hash>') as client:
result = client(functions.contacts.ResolveUsernameRequest(
    username=<username>
))
print(result.stringify())
Contretemps answered 23/4, 2023 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.