调用电报 API 方法的正确方法

2022-09-04 23:29:09

我正在我的java应用程序中研究Telegram api。我需要使用我的电报帐户进行身份验证和授权,并获取我的特定组的消息列表。为此,我首先从电报站点获得。其次,我试图以这种方式用方法授权我的帐户:api_idapi_hashMTProto serversauth.sendCode

...
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);
...

但这又让我例外。我测试了网址而不是,这返回了html内容。在java中调用电报API方法的正确方法是什么?javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshakehttphttps404 Not Found

更新:

我尝试使用发送TCP发布请求,但这会返回我。java socket404 not found


答案 1

由于它是mproto协议,因此您必须遵守其规范 - https://core.telegram.org/mtproto

我建议你使用这个项目,因为它有工作的例子 - https://github.com/badoualy/kotlogram


答案 2

推荐