【问题标题】:Correct way to call telegram api methods调用电报api方法的正确方法
【发布时间】:2017-02-26 05:36:37
【问题描述】:

我正在我的 java 应用程序中处理 Telegram api。 我需要对我的电报帐户进行身份验证和授权,并获取我的特定组的消息列表。 为此,我首先从电报网站获得了api_idapi_hashMTProto servers。其次,我尝试以这种方式使用auth.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);
...

但这会给我带来javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake 异常。我用http 而不是https 测试了url,这返回了404 Not Found html 内容。 java中调用电报api方法的正确方法是什么?

更新:

我尝试使用 java socket 发送 TCP 发布请求,但这返回给我 404 not found

【问题讨论】:

  • 您是否查看了使用 API 的库?例如。 github.com/ex3ndr/telegram-api ...
  • @Fildor 如果可能,我不想使用第 3 方库。我想使用 main java 方法来做到这一点。
  • 不,不使用它。看看,那里是怎么做的……
  • https://149.154.167.40:443 - 你确定这是一个有效的 IP/端口吗?
  • @Fildor Telegram 网站向我提供了该 IP 地址。

标签: java telegram


【解决方案1】:

既然是 mproto 协议,你必须遵守他们的规范 - https://core.telegram.org/mtproto

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

【讨论】:

  • 感谢您的回复。如果可能的话,我想自己做,没有任何第三方库。能否请您提供一个方法to post data to telegram mtproto server? 如果我知道这一点,我可以完成所有剩余的工作。
  • @CharlesOkwuagwu 我的问题是没有生成授权密钥。我对 auth-key 没有任何问题!我的问题是将发布请求发送到电报 mtproto 服务器。我想找到一种正确的方法在我的 java 应用程序中将数据发布到电报服务器,而不是生成 auth-key!
  • @hamed 我试图向您解释,与 MTproto 的通信不能直接通过 POST 请求完成,就像您尝试的方式一样。我进一步向您指出了一些资源,这些资源将引导您了解如何开始与电报服务器“对话”。
  • @hamed - 你可以研究这个项目的源代码github.com/badoualy/kotlogram,但它内部默认使用TCP连接到电报服务器。但是你也可以学习官方的 Telegram 桌面客户端源代码 - github.com/telegramdesktop/tdesktop/blob/master/Telegram/… 支持 HTTP 协议,你可以在 Telegram 桌面客户端设置中看到它 - postimg.org/image/g3kjwhi8d
猜你喜欢
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 2011-04-15
  • 2016-10-12
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多