sochix / TLSharp

Telegram client library implemented in C#
1k stars 380 forks source link

Trouble connecting and authing #587

Open ghost opened 7 years ago

ghost commented 7 years ago

Hi, I get this exception: InvalidOperationException: AUTH_KEY_UNREGISTERED

client.IsUserAuthorized() returns true.

StackTrace " at TLSharp.Core.Network.MtProtoSender.HandleRpcResult(UInt64 messageId, Int32 sequence, BinaryReader messageReader, TLMethod request) in C:\\Users\\Harmen\\Desktop\\MyApp\\TLSharp-master\\TLSharp.Core\\Network\\MtProtoSender.cs:line 318" string

Windows Form

        private void Form1_Load(object sender, EventArgs e)
        {            
            var t= Task.Run(() => connectTelegram());
            t.Wait();

            var t3 = Task.Run(() => makeAuthAsync());
            t3.Wait();

            var t2 = Task.Run(() => getHistoryAsync());
            t2.Wait();        
        }

Connect method (try to load session from file)

        private async System.Threading.Tasks.Task connectTelegram()
        {
            int apiId = 12345678;
            String apiHash = "My api hash";
            var store = new FileSessionStore();

            client = new TelegramClient(apiId, apiHash, store);
            await client.ConnectAsync();

            bool authorized = client.IsUserAuthorized();
            if (!authorized)
            {
                await makeAuthAsync();
            }
            else
            {
                await getHistoryAsync();
            }
        }

Authentification

        private async System.Threading.Tasks.Task makeAuthAsync()
        {

            var hash = await client.SendCodeRequestAsync("+My phone number");
            var code = "<code_from_telegram>"; // you can change code in debugger

            var user = await client.MakeAuthAsync("+My Phone number", hash, code);
        }

Try to get message history of certain channel (this is my goal)

        private async System.Threading.Tasks.Task getHistoryAsync()
        {
            var dialogs = (TLDialogs)await client.GetUserDialogsAsync();
            var chat = dialogs.chats.lists
                .OfType<TLChannel>()
                .FirstOrDefault(c => c.title == "Channelname");

            var tlInputPeerChannel = new TLInputPeerChannel() { channel_id = chat.id, access_hash = chat.access_hash.Value };
            var tlAbsMessages = await client.GetHistoryAsync(tlInputPeerChannel, 0, 10000, 10000);
            var tlChannelMessages = (TLChannelMessages)tlAbsMessages;

            System.Collections.Generic.List<int> messageIds = new List<int>();

            foreach (TLAbsMessage message in tlChannelMessages.messages.lists)
            {
                var tlMessage = message as TLMessage;
                messageIds.Add(tlMessage.id);
            }
        }

If I skip the IsUserAuthorized check and go straight to makeAuthSync() and I put the recieved code via the debugger in the code than I get InvalidOperationException: Couldn't read the packet length StackTrace " at TLSharp.Core.Network.TcpTransport.<Receieve>d__4.MoveNext() in C:\\Users\\Harmen\\Desktop\\MyApp\\TLSharp-master\\TLSharp.Core\\Network\\TcpTransport.cs:line 45" string

Any advice is welcome :)

i3130002 commented 7 years ago

In my experience I re register the user and then the code worked fine.

Here is the working code of mine

var store = new FileSessionStore();
client = new TelegramClient(int.Parse(textBox1.Text), textBox2.Text, store);
await client.ConnectAsync();
if (client.IsUserAuthorized())
{ you are set }
else
{ go and autenticate}