SASL authorization failing while connecting to XMPP server
Asked Answered
M

3

5

I am trying to connect to gmail using SMACK API through XMPP server. but getting the

error : SASL authentication failed using mechanism PLAIN

you can check a glimpse of code. I got it from net only

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

I checked in the smack debug window. it says in XML :

< invalid-authzid />

I am already having account on gmail and my gtalk is also running.

Multistage answered 17/9, 2010 at 9:20 Comment(0)
K
9

You need to set the authentication before you connect viz

SASLAuthentication.supportSASLMechanism("PLAIN", 0);

must appear before connection.connect().

See my blog.

Killebrew answered 20/9, 2010 at 0:47 Comment(2)
I cannot find that method in that class. Any alternative?Gauhati
This method does not belong to my version of smackTrinary
R
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();
    }

I also get this mistake, but i can not work.

Richmound answered 19/7, 2012 at 4:17 Comment(2)
Explain whats the error and provide correction to the user provided code.Finfoot
SASLAuthentication.supportSASLMechanism("PLAIN", 0); is not available anymore.Gauhati
P
0

For anyone looking for possible solutions to this many years after this was originally asked and answered, I recently was able to get past this authentication error by explicitly setting the authzid value on the XMPPTCPConnectionConfiguration.

I was running into an issue where my connection configuration worked fine for some client XMPP servers, but not for others, even though they were all using SASL PLAIN authentication. After some troubleshooting, I learned that the ones that were failing were expecting an authzid value. After adjusting my code to set this, it works in both the environments that were working before, as well as the environments that were failing.

Here is how I am building my connection configuration:

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();

Specifically I needed to add this line:

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

Ptah answered 21/12, 2019 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.