java - 如何向服务器验证我的 XMPP 客户端?
问题描述
我正在为一个大学项目编写一个 XMPP 客户端,该项目应该发送和接收来自其他客户端的消息。我对 XMPP 及其语法有基本的了解,但似乎无法连接到 gtalk 服务器。免责声明 - 我不能使用 Smack 或任何其他库。
Socket s = new Socket("talk.l.google.com", 5222);
PrintWriter out = new PrintWriter(s.getOutputStream());
out.println("<?xml version='1.0' encoding='utf-8' ?>");
out.println("<stream:stream "
+ "xmlns='jabber:client' "
+" from='example@gmail.com' to='gmail.com' "
+ " xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>");
out.flush();
我假设连接成功,因为我得到了这个响应:
<stream:stream from="gmail.com" id="E3A7EFC5647601B3" version="1.0" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client"><stream:features><starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"><required/></starttls><mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><mechanism>X-OAUTH2</mechanism><mechanism>X-GOOGLE-TOKEN</mechanism></mechanisms></stream:features>
如果我尝试发送一些 xml 数据包,例如一条消息,我显然会得到一个
<stream:error><not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-streams"/></stream:error>
我这样发送消息:
out.println("<message to='07xcpozn829nd25ivx1gpp3dug@public.talk.google.com'"
+"from='example@gmail.com'>\r\n"
+"<body>Wherefore art thou?</body>\r\n"
+"</message>");
- https://xmpp.org/rfcs/rfc6120.html#sasl说,未授权错误与我未通过服务器身份验证有关。我浏览了 google、stackoverflow、xmpp 的原始 rfc,但似乎找不到任何关于如何通过服务器验证自己的信息。我似乎也无法在 stackoverflow 或任何其他平台上找到任何示例代码,因为每个人都只是建议使用 Smack 或任何其他 xmpp 库。
- 关于流协商部分的另一件事是,我的部分有一个“来自”属性,这应该意味着服务器响应应该包含一个“到”属性,但事实并非如此。例子:
I: <?xml version='1.0'?>
<stream:stream
from='juliet@im.example.com'
to='im.example.com'
version='1.0'
xml:lang='en'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'>
R: <?xml version='1.0'?>
<stream:stream
from='im.example.com'
id='++TR84Sm6A3hnt3Q065SnAbbk3Y='
to='juliet@im.example.com'
version='1.0'
xml:lang='en'
xmlns='jabber:client'
xmlns:stream='http://etherx.jabber.org/streams'>
解决方案
在 SASL 协商过程中,您一定错过了身份验证部分。
检查第9.1 章。https://datatracker.ietf.org/doc/rfc6120/中的客户端到服务器示例