14

I'm working on Telegram api in my java application. I need to do authentication and authorization with my telegram account and get message list of my specific group. For this purpose, first I got api_id, api_hash and MTProto servers from telegram site. Second, I tried to authorize my account with auth.sendCode method in this way:

...
String url = "https://149.154.167.40:443/auth.sendCode";
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.addHeader("charset", "UTF-8");

List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("phone_number", myPhoneNumber));
nameValuePairs.add(new BasicNameValuePair("sms_type", "5"));
nameValuePairs.add(new BasicNameValuePair("api_id", api_id));
nameValuePairs.add(new BasicNameValuePair("api_hash", api_hash));
nameValuePairs.add(new BasicNameValuePair("lang_code", "en"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

HttpResponse response = httpClient.execute(httpPost);
...

But this returns me javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake exception. I tested url with http instead of https and this returned 404 Not Found html content. What is the correct way for calling telegram api method in java?

Update:

I tried using java socket for sending TCP post request, but this returns me 404 not found.

Gimby
  • 5,095
  • 2
  • 35
  • 47
hamed
  • 7,939
  • 15
  • 60
  • 114
  • 2
    Did you have a look into libs using the API? E.g. https://github.com/ex3ndr/telegram-api ... – Fildor Oct 17 '16 at 09:47
  • @Fildor I don't want to use 3rd party library if possible. I want to use main java method for doing that. – hamed Oct 17 '16 at 09:59
  • No, not use it. Just look, how it's done there ... – Fildor Oct 17 '16 at 10:00
  • `https://149.154.167.40:443` - Are you sure this is a valid IP/Port ? – Fildor Oct 17 '16 at 10:11
  • @Fildor Telegram site provided me that ip address. – hamed Oct 17 '16 at 10:18
  • @CharlesOkwuagwu Sending post request through TCP instead of HTTP can solve the problem? – hamed Oct 17 '16 at 10:20
  • @CharlesOkwuagwu I used java sockets for posting via TCP, but no luck. just returns 404 not found. – hamed Oct 19 '16 at 04:07
  • @hamed There are two classes of MTProto methods: Encrypted and Plain. `auth.sendCode` is an Encrypted request. Before you can get to the stage of sending encrypted messages you need a valid **session** running in one of Telegram DCs (Data Centers). To get yourself a valid session, you need to go through the process of creating an Auth-Key. Do you follow? – Charles Okwuagwu Oct 24 '16 at 17:47
  • have you found any solution? Please let me know if you have solved it. – Anand Savjani Oct 07 '22 at 19:47

1 Answers1

1

Since it's mproto protocol, you must obey their specification - https://core.telegram.org/mtproto

I suggest you to use this project, since it has working examples - https://github.com/badoualy/kotlogram

joojic
  • 66
  • 2
  • Thank you for your response. I want to do it myself, without any third-party library, if possible. Could you please provide me just a way `to post data to telegram mtproto server?` If I know this, I can do all of the remaining work. – hamed Oct 22 '16 at 04:57
  • @CharlesOkwuagwu My problem isn't generating auth-key. I don't have any problem with auth-key! my problem is sending post request to telegram mtproto server. I want to find a correct way to post data to telegram servers in my java application, not generating auth-key! – hamed Oct 23 '16 at 05:14
  • @hamed i have tried to explain to you that communicating with MTproto cannot be done directly via POST requests, the way you are trying to go. I further pointed you to resources that walk you through how to get started "speaking" to telegram servers. – Charles Okwuagwu Oct 23 '16 at 05:30
  • 1
    @hamed - You can study source code of this project https://github.com/badoualy/kotlogram, but it internally uses TCP connection by default to telegram server. But you can also study source code of official telegram desktop client - https://github.com/telegramdesktop/tdesktop/blob/master/Telegram/SourceFiles/mtproto/connection_http.cpp which has support for HTTP protocol, you can see it in Telegram Desktop Client Settings - https://postimg.org/image/g3kjwhi8d/ – joojic Oct 23 '16 at 17:43