使用 smack 连接到 Google Talk
Posted
技术标签:
【中文标题】使用 smack 连接到 Google Talk【英文标题】:Connect to Google Talk using smack 【发布时间】:2012-08-01 09:32:46 【问题描述】:我想开发一个连接到 Google Talk 并允许用户与其朋友聊天的 Java 应用程序。我正在使用 smack API 和下面的代码:
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();
但我得到了休闲异常:
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)
谁能帮我解决这个问题?
【问题讨论】:
【参考方案1】:这应该可以解决问题,非常简单
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");
【讨论】:
【参考方案2】:这是我使用 smack 连接到 google talk 的方法。
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);
和身份验证。
public void authenticate(String username, String password) throws Exception
xmppConnection.login(username, password);
buddyList.setSession(xmppConnection);
setStatus(ConnectionStatus.AUTHENITCATED);
【讨论】:
谢谢,我成功登录了,但由于某种原因,我得到了一个异常,我不知道如何发布它 可以登录但出现异常?能发第一行吗?【参考方案3】:这拯救了我的一天
connectionConfig = new ConnectionConfiguration(host, port, service);
connectionConfig.setSASLAuthenticationEnabled(false);
connectionConfig.setTruststoreType("BKS");
connection = new XMPPConnection(connectionConfig);
【讨论】:
以上是关于使用 smack 连接到 Google Talk的主要内容,如果未能解决你的问题,请参考以下文章
Smack 连接到 Google 的 XMPP 服务 (gtalk) 会引发异常“SASLError using PLAIN: not-authorized”
使用 smack 和 servlet 连接到 facebook 聊天时
是否可以使用 BOSH 连接到 google talk xmpp 服务器?