5

我正在尝试通过 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 也在运行。

4

3 回答 3

9

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

SASLAuthentication.supportSASLMechanism("PLAIN", 0);

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

见我的博客

于 2010-09-20T00:47:13.960 回答
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();
    }

我也犯了这个错误,但我无法工作。

于 2012-07-19T04:17:16.103 回答
0

对于在最初被询问和回答多年后寻找可能解决方案的任何人,我最近能够通过authzidXMPPTCPConnectionConfiguration.

我遇到了一个问题,我的连接配置对于某些客户端 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)))

于 2019-12-21T02:32:37.493 回答