Connect to Google Talk using smack
Asked Answered
I

3

6

I want to develop a Java application which connects to Google Talk and allows a user to chat with it's friends. I am using smack API and the fallowing code:

ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com",5222,"gmail.com");
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
XMPPConnection connection  = new XMPPConnection(config);
try {
    connection.connect();
} catch (XMPPException e) {
    e.printStackTrace();
}
try {
    connection.login("username", "password");
} catch (XMPPException e) {
    e.printStackTrace();
}

but I obtain the fallowing exception:

SASL authentication PLAIN failed: invalid-authzid: 
    at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:337)
    at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:203)
    at org.jivesoftware.smack.Connection.login(Connection.java:348)
    at Main.main(Main.java:21)

Can someone help me to solve this problem?

Involve answered 1/8, 2012 at 9:32 Comment(0)
J
5

This should do the trick, quite simple

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 SenderTest 
{
public static void main(String args[])
{
    //ConnectionConfiguration connConfig = new ConnectionConfiguration("localhost", 5222);
        //connConfig.setSASLAuthenticationEnabled(false);
     //ConnectionConfiguration connConfig = new ConnectionConfiguration("localhost", 5222);
     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("[email protected]", "a");
            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 newChat = chatmanager.createChat("[email protected]", new MessageListener() {
        public void processMessage(Chat chat, Message message) {
            System.out.println("Received message: " + message);
        }
    });

    try {
        newChat.sendMessage("Howdy!");
        System.out.println("Message Sent...");
    }
    catch (XMPPException e) {
        System.out.println("Error Delivering block");
    }
}

}
Janson answered 7/8, 2012 at 8:55 Comment(0)
N
1

This is the method I used to connect to google talk using smack.

 private ConnectionStatus status;
 private XMPPConnection xmppConnection;

public void connect(String server, int port, String s) throws Exception
{
xmppConnection = new XMPPConnection(new ConnectionConfiguration(server, port,s));
xmppConnection.connect();
xmppConnection.addConnectionListener(this);
xmppConnection.getChatManager().addChatListener(this); 
}

and the authentication.

public void authenticate(String username, String password) throws Exception
{
  xmppConnection.login(username, password);
buddyList.setSession(xmppConnection);
setStatus(ConnectionStatus.AUTHENITCATED);
}
Naucratis answered 1/8, 2012 at 10:40 Comment(2)
Thank, I managed to login but for some reason I obtaining an Exception and I don't know how to post itInvolve
You can log in but get an exception?can you post the first line?Naucratis
Z
1

This saved my day

connectionConfig = new ConnectionConfiguration(host, port, service);
connectionConfig.setSASLAuthenticationEnabled(false);
connectionConfig.setTruststoreType("BKS");
connection = new XMPPConnection(connectionConfig);
Zug answered 30/3, 2014 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.