使用smack连接到Google Talk [英] Connect to Google Talk using smack

查看:96
本文介绍了使用smack连接到Google Talk的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个连接到Google Talk的Java应用程序,允许用户与其朋友聊天。我正在使用smack API和以下代码:

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?

推荐答案

这应该可以解决问题,非常简单

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("sender@example.com", "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("receiver@gmail.com", 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");
    }
}

}

这篇关于使用smack连接到Google Talk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆