【问题标题】:Smack 4.1 connection to gtalk与 gtalk 的 Smack 4.1 连接
【发布时间】:2015-04-08 14:53:59
【问题描述】:

我更新到 smack 4.1,但现在无法连接到 gtalk。这是我正在使用的代码:

XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
    configBuilder.setHost("talk.google.com");       
    configBuilder.setPort(5222);
    configBuilder.setServiceName("gmail.com");
    configBuilder.setSecurityMode(SecurityMode.required);
    configBuilder.setDebuggerEnabled(true);
    configBuilder.setSendPresence(true);  
    configBuilder.setUsernameAndPassword(pref.getString(Constants.KEY_USER, ""), pref.getString(Constants.KEY_TOKEN, ""));

    SASLAuthentication.blacklistSASLMechanism(SASLMechanism.PLAIN);
    AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
    try
    {
        connection.connect();
        connection.login();//.login(pref.getString(Constants.KEY_USER, ""), pref.getString(Constants.KEY_TOKEN, ""));
    }
    catch(Exception e)
    {
        e.printStackTrace();
        return null;
    }

这是我得到的错误:

D/SMACK(12807): SENT (3): <auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='X-OAUTH2'>REMOVED_THIS=</auth>
D/SMACK(12807): RECV (3): <failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><incorrect-encoding/></failure>

【问题讨论】:

    标签: smack


    【解决方案1】:

    问题出在 SASLXOauth2Mechanism 中的一行:

    Base64.encode(toBytes('\u0000' + authenticationId + '\u0000' + password));
    

    作为代码中的快速测试替换

    AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build());
    

    有了这个

        AbstractXMPPConnection connection = new XMPPTCPConnection(configBuilder.build())
        {
            @Override
            public void send(PlainStreamElement auth) throws NotConnectedException
            {
                if(auth instanceof AuthMechanism)
                {           
                    final XmlStringBuilder xml = new XmlStringBuilder();
                    xml.halfOpenElement(AuthMechanism.ELEMENT)
                    .xmlnsAttribute(SaslStreamElements.NAMESPACE)
                    .attribute("mechanism", "X-OAUTH2")
                    .attribute("auth:service", "oauth2")
                    .attribute("xmlns:auth", "http://www.google.com/talk/protocol/auth")
                    .rightAngleBracket()
                    .optAppend(Base64.encodeToString(StringUtils.toBytes("\0" + authenticationId + "\0" + password)))
                    .closeElement(AuthMechanism.ELEMENT);                   
                     super.send(new PlainStreamElement()
                     {
                        @Override
                        public String toXML()
                        {
                            return xml.toString();
                        }                        
                     });                 
                }   
                else super.send(auth);
            }
        };
    

    我没有测试它,但我希望它有效。 authenticationId 和 token 是您的凭据。

    【讨论】:

    • 谢谢,它对我有用。这似乎是 smack 中一个很容易修复的错误,不是吗?
    • 它也对我有用。感谢您的贡献。
    • 谢谢,拯救了我的一天 :)
    猜你喜欢
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 2015-07-22
    • 2012-12-28
    • 2015-09-28
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多