首页 > 解决方案 > 如何向服务器验证我的 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>");

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'>

标签: javasocketsauthenticationxmppgoogle-talk

解决方案


在 SASL 协商过程中,您一定错过了身份验证部分。

检查第9.1 章。https://datatracker.ietf.org/doc/rfc6120/的客户端到服务器示例


推荐阅读