powered by Jive Software

Example for GoogleTalk with Smack 3.0.x

Hi,

I had smack 2.2.1 (somehow) working with Google Talk.

I’'ve tried to upgrade but I now get an IllegalStateException…

This is my code (modified ‘‘guessed’’ for 3.0.2)

XMPPConnection gtalk = null;

ConnectionConfiguration conf = new ConnectionConfiguration(“talk.google.com”, 5222, “googlemail.com”);

gtalk = new XMPPConnection(conf);

gtalk.login(“myGoogleIdWithout@”, “myPassword”, “smack”);

Chat chat = gtalk.getChatManager().getThreadChat("myfriend@googlemail.com");

chat.sendMessage(“HI Dude”);

I get this:

java.lang.IllegalStateException: Not connected to server.

at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:329)

at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:301)

Could somebody post a working version end-2-end to simply: connect (googlemail please), create a chat and send a message to someone…

Many thanks!!!

Benoit.

From the XMMPConnection Javadocs…

Note that XMPPConnection constructors do not establish a connection to the server and you must call connect()

Call connect() before login

Here’'s a working GTalk example I wrote with Smack 3.0.2

import java.io.IOException;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence; public class SendTest {
        public static class MessageParrot implements MessageListener {
                private Message msg = new Message("macro10.com@gmail.com", Message.Type.chat);
                // gtalk seems to refuse non-chat messages
        // messages without bodies seem to be caused by things like typing
        public void processMessage(Chat chat, Message message) {
            if(message.getType().equals(Message.Type.chat) && message.getBody() != null) {
                System.out.println("Received: " + message.getBody());
                try {
                    msg.setBody("I am a Java bot. You said: " + message.getBody());
                    chat.sendMessage(msg);
                } catch (XMPPException ex) {
                    //ex.printStackTrace();
                    System.out.println("Failed to send message");
                }
            } else {
                System.out.println("I got a message I didn''t understand");
            }
        }
    }
            public static void main( String[] args ) {
                System.out.println("Starting IM client");
                // gtalk requires this or your messages bounce back as errors
        ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
        XMPPConnection connection = new XMPPConnection(connConfig);
                try {
            connection.connect();
            System.out.println("Connected to " + connection.getHost());
        } catch (XMPPException ex) {
            //ex.printStackTrace();
            System.out.println("Failed to connect to " + connection.getHost());
            System.exit(1);
        }
        try {
            connection.login("USERNAME", "PASSWORD");
            System.out.println("Logged in as " + connection.getUser());
                        Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
                    } catch (XMPPException ex) {
            //ex.printStackTrace();
            System.out.println("Failed to log in as " + connection.getUser());
            System.exit(1);
        }
                ChatManager chatmanager = connection.getChatManager();
        Chat chat = chatmanager.createChat("macro10.com@gmail.com", new MessageParrot());
                try {
            // google bounces back the default message types, you must use chat
            Message msg = new Message("macro10.com@gmail.com", Message.Type.chat);
            msg.setBody("Test");
            chat.sendMessage(msg);
        } catch (XMPPException e) {
            System.out.println("Failed to send message");
            // handle this how?
        }
                System.out.println("Press enter to disconnect");
                try {
            System.in.read();
        } catch (IOException ex) {
            //ex.printStackTrace();
        }
                connection.disconnect();      }
}

Hi,

Thanks for the sample codes. I changed the login name and password and managed to run it in my Eclipse 3.2 with JDK 1.5.x, but it returned this error:

Starting IM client

Connected to talk.google.com

Failed to log in as null

Why is there a “null” there? Did I missed to replace something else?

Please help.

Regards,

Sylver

hi,

i have also the same problem with the null.

when i examine the exception in debug i found out that the null refers to the fact that the value for the server is null.

The question is which server? mine which cannot be resolved?

I am having exactly the same problems! have spend ages trying to figure it out but can’‘t see what’'s wrong?

The example I gave really does work, I promise. The null was simply because the XMPPConnection does something I don’‘t think it should, it only stores the username for successful logins. I think it’‘s a bug in Smack but it’'s one of those kind of things that the Smack developers might call a feature.

After using Smack for a little while I’‘ve found the Chat class to be kind of worthless so I’‘ve modified the example to avoid using it. I’‘ve also created a junk gtalk account so I could leave the username and password values in the example. If you had a problem with the last example it’'s probably because you were using username@gmail.com instead of just username. Run this example with

java SendTest

to just wait for messages and auto-reply to them. Run with

java SendTest another.user@gmail.com

to do the same but also send a single message to start the conversation.

This example runs with zero modifications but remember if you don’'t change the username and password values then everyone running the example is going to be logged in at the same time and I think only one of you is going to get the messages.

import java.io.IOException;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils; public class SendTest {
        // Notice the username is NOT smack.test@gmail.com
    private static String username = "smack.test";
    private static String password = "ignite";
        public static class MessageParrot implements PacketListener {
        private XMPPConnection xmppConnection;
                public MessageParrot(XMPPConnection conn) {
            xmppConnection = conn;
        }
                public void processPacket(Packet packet) {
            Message message = (Message)packet;
            if(message.getBody() != null) {
                String fromName = StringUtils.parseBareAddress(message.getFrom());
                System.out.println("Message from " + fromName + "\n" + message.getBody() + "\n");
                Message reply = new Message();
                reply.setTo(fromName);
                reply.setBody("I am a Java bot. You said: " + message.getBody());
                xmppConnection.sendPacket(reply);
            }
        }
    };
            public static void main( String[] args ) {
                System.out.println("Starting IM client");
                // gtalk requires this or your messages bounce back as errors
        ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com");
        XMPPConnection connection = new XMPPConnection(connConfig);
                try {
            connection.connect();
            System.out.println("Connected to " + connection.getHost());
        } catch (XMPPException ex) {
            //ex.printStackTrace();
            System.out.println("Failed to connect to " + connection.getHost());
            System.exit(1);
        }
        try {
            connection.login(username, password);
            System.out.println("Logged in as " + connection.getUser());
                        Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
                    } catch (XMPPException ex) {
            //ex.printStackTrace();
            // XMPPConnection only remember the username if login is succesful
            // so we can''t use connection.getUser() unless we log in correctly
            System.out.println("Failed to log in as " + username);
            System.exit(1);
        }
                PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
        connection.addPacketListener(new MessageParrot(connection), filter);
                if(args.length > 0) {
            // google bounces back the default message types, you must use chat
            Message msg = new Message(args[0], Message.Type.chat);
            msg.setBody("Test");
            connection.sendPacket(msg);
        }
                System.out.println("Press enter to disconnect\n");
                try {
            System.in.read();
        } catch (IOException ex) {
            //ex.printStackTrace();
        }
                connection.disconnect();
    }
}

This example still doesn’'t work for me? I get the following outcome:

Starting IM client

Connected to talk.google.com

Failed to log in as smack.test

Java Result: 1

And I get the same outcome when I connect with my own gmail account?

I am in London… is it something to do with having a connection in europe? This is really strange… I am using a mac and java version:

Java™ 2 Runtime Environment, Standard Edition (build 1.5.0_07-164)

By the way, I am using the latest available version of smack api, 3.0.3

I’‘m using Smack 3.0.2, Windows XP, and JDK 1.6 but it might be a location problem. I’'ve only tried at locations within the United States. I think in the UK and in Germany there was an issue with the GMail.com domain what about if you try

ConnectionConfiguration conf = new ConnectionConfiguration("talk.google.com", 5222, "googlemail.com");

hmm… still not working? Maybe it’'s the version of java for mac that I have? Is it working for everyone else now?

I doubt it’‘s your Java as long as it runs and compiles. You tried with the googlemail.com domain and an account created in the UK? I’‘d suggest creating a jabber.org account and modifying the connection info, username, and password just to make sure this is a server issue and nothing with your environment. Once that’‘s working then use a IM client like pidgin to figure out the settings you need in the UK for gtalk. If anyone has a UK computer that I can have remote access to (RDP/VNC) with Java I’‘d be more than happy to work on this problem but there doesn’'t seem to be anything else I can do from my current location.

If I use:

ConnectionConfiguration connConfig = new ConnectionConfiguration(“jabber.org”, 5222, “jabber.org”);

and use my jabber.org username and password, it connects fine! Shall I email you the ssh details of my host and you can try to see if you can get it working on there?

I’'m getting the SASL authentication failed errors as the others reported when using 3.0.3. Everything works fine when using the 2.2.1 libraries.

It seems the code posted doesn’'t work. Anybody have 3.0.3 working code?

ConnectionConfiguration config = new ConnectionConfiguration(

talk.google.com”, 5222, “gmail.com”);

XMPPConnection con = new XMPPConnection(config);

try {

con.connect();

// username@gmail.com google talk account

con.login(username, password);

} catch (XMPPException ex) {

}

con.disconnect();

Just want to confirm that with 3.0.3 it does seem broken. I get this:

SASL authentication failed:
        at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:209)
        at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:341)
        at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:301)
        at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:283)
        at test.SendTest.main(SendTest.java:85)

It’‘s probably related to http://www.igniterealtime.org/issues/browse/SMACK-224 which was listed as part of the changelog for 3.0.3. I’‘m not sure if it’'s a Smack bug or if there should be something corrected in my example. Just stick with the old version for now http://www.igniterealtime.org/downloads/download-landing.jsp?file=smack/smack_3_ 0_2.zip

Hey guys,

Thanks for the bug report. I re-opened SMACK-224 and marked it to be fixed for the next release. I’'m going to have to figure out some way to make both SSO and Google work, though.

-Matt

It was a simple fix. The PLAIN mech was using an authentication ID equal to the username@hostname. In prior versions the hostname was set to the xmpp domain, but to make things work with SSO it needs to be the FQDN of the server, which may not match (indeed, it often wont). But as I said, the solution is simple. The PLAIN mechanism dosnt require an authentication ID, and in fact the way its being used in Smack wouldnt allow you to do anything useful with it anyway. So we just made that field empty.

BTW, I just released a new Smack beta that includes the fix. Please let me know how it works for people!

-Matt

Both GTalk and a local Openfire seem to be working just fine for me with the beta. Thanks!

I encountered the same problem:

Starting IM client

Connected to talk.google.com

Failed to log in as smack.test

it happened both on my own account and the account given

I am in canada and I use JAVA 1.6 + SMACK 3.0.2

Chase, Can you also explain why there no SASL authentication in your code? seems like googe talk requries SASL?

Cheers,

Cooler