SASL授权在连接到XMPP服务器时失败

问题描述 投票:4回答:3

我正在尝试通过XMPP服务器使用SMACK API连接到gmail。但是得到

错误:使用机制PLAIN的SASL身份验证失败

您可以查看一下代码。我只是从网上得到的

ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
connection = new XMPPConnection(connConfig);
connection.connect();
SASLAuthentication.supportSASLMechanism("PLAIN", 0);

我在smack调试窗口中检查了。它用XML表示:

我已经在gmail上拥有帐户,并且我的gtalk也正在运行。

xmpp smack google-talk
3个回答
9
投票

连接前,您需要设置身份验证

SASLAuthentication.supportSASLMechanism("PLAIN", 0);

必须出现在connection.connect()之前。

请参阅我的blog


1
投票
    ConnectionConfiguration cc = new ConnectionConfiguration(
            "vietnam.agilemobile.com", 5222, vietnam.agilemobile.com");
    XMPPConnection connection = new XMPPConnection(cc);
    try {
        SASLAuthentication.supportSASLMechanism("PLAIN", 0);
        connection.connect();
        Log.e("LOGIN", "" + 111);
        // You have to put this code before you login
        Log.e("LOGIN", "" + 222);
        // You have to specify your gmail addres WITH @gmail.com at the end
        connection.login("nemodo", "123456", "resource");
        Log.e("LOGIN", "" + 333);
        // See if you are authenticated
        System.out.println(connection.isAuthenticated());

    } catch (XMPPException e1) {
        e1.printStackTrace();
    }

我也遇到这个错误,但是我不能工作。


0
投票

对于最初被问及得到答案之后多年寻求解决方案的人,我最近可以通过在authzid上明确设置XMPPTCPConnectionConfiguration值来克服此身份验证错误。

我遇到了一个问题,即使某些客户端XMPP服务器都使用SASL PLAIN身份验证,我的连接配置也可以正常工作。经过一些故障排除后,我了解到那些失败的期望值是authzid。调整好代码以进行设置后,它既可以在以前的环境中工作,也可以在失败的环境中工作。

这是我建立连接配置的方式:

XMPPTCPConnectionConfiguration.builder()
                              .setHost(XMPP_DOMAIN)
                              .setXmppDomain(XMPP_DOMAIN)
                              .setPort(XMPP_PORT)
                              .setCompressionEnabled(true) // optional, not all servers will support this
                              .setUsernameAndPassword(XMPP_USER, XMPP_PASSWORD)
                              .setResource(XMPP_RESOURCE)
                              .setAuthzid(JidCreate.entityBareFrom(String.format("%s@%s", XMPP_USER, XMPP_DOMAIN))) // <-- this was the change I needed
                              .build();

特别是我需要添加此行:

.setAuthzid(JidCreate.entityBareFrom(String.format("%s@%s", XMPP_USER, XMPP_DOMAIN)))

© www.soinside.com 2019 - 2024. All rights reserved.