使用JAVA中的Smack API连接到Google日历

使用JAVA中的Smack API连接到Google日历,java,google-calendar-api,smack,Java,Google Calendar Api,Smack,有人能告诉我如何使用Smack API连接到谷歌日历服务器吗?如果我可以的话。使用Smack连接到谷歌日历是什么意思?Smack将允许您连接到XMPP服务器,这是一种即时消息传递方式。如果您想要类似的东西,那么有两个方面:实现bot,然后连接到它并与它交谈。 但一般来说,这里有一个代码示例,可以让您连接到GTalk: import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ChatManager; import org.

有人能告诉我如何使用Smack API连接到谷歌日历服务器吗?如果我可以的话。

使用Smack连接到谷歌日历是什么意思?Smack将允许您连接到XMPP服务器,这是一种即时消息传递方式。如果您想要类似的东西,那么有两个方面:实现bot,然后连接到它并与它交谈。 但一般来说,这里有一个代码示例,可以让您连接到GTalk:

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.proxy.ProxyInfo;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.packet.Presence.Type;

public class Communicator {
    public static final String XMPP_SERVER = "talk.google.com";
    public static final String XMPP_HOST_NAME = "gmail.com";
    public static final String XMPP_SERVICE_NAME = "gmail.com";
    public static final int PACKET_REPLY_TIMEOUT = 500, DEFAULT_XMPP_SERVER_PORT = 5222;

    XMPPConnection conn;
    Roster buddyList;

    public static String canonicalizeUsername(String username) {
        if (!username.contains("@")) {
            username += "@" + XMPP_SERVICE_NAME;
        }
        return username;
    }

    public Communicator(String username, String password) throws XMPPException {
        this(XMPP_SERVER, DEFAULT_XMPP_SERVER_PORT, username, password);
    }

    public Communicator(String serverAddress, Integer serverPort, String username,
                        String password) throws XMPPException {
        username = canonicalizeUsername(username);
        SmackConfiguration.setPacketReplyTimeout(PACKET_REPLY_TIMEOUT);
        ConnectionConfiguration config =
                  new ConnectionConfiguration(serverAddress, serverPort != null ? serverPort : DEFAULT_XMPP_SERVER_PORT,
                                              XMPP_HOST_NAME, ProxyInfo.forDefaultProxy());
        //config.setSASLAuthenticationEnabled(true);
        //config.setSecurityMode(SecurityMode.disabled);
        //SASLAuthentication.supportSASLMechanism("PLAIN");
        conn = new XMPPConnection(config);
        conn.connect();
        System.out.println("Connected to " + serverAddress + ":" + serverPort);
        conn.login(username, password);
        System.out.println("Logged in as " + username);
        setStatus(true, "ON");
    }

    public void setStatus(boolean available, String status) {
        Presence presence = new Presence(available ? Type.available : Type.unavailable);
        presence.setStatus(status);
        conn.sendPacket(presence);
    }

    public void destroy() throws Exception {
        conn.disconnect();
    }

    public boolean sendMessage(String msgText, String to) throws XMPPException {
        to = canonicalizeUsername(to);
        ChatManager mgr = conn.getChatManager();
        Chat chat = mgr.createChat(to, new MessageListener() {
                public void processMessage(Chat chat, Message msg) {
                    System.out.println(msg.getBody());
                }
            });
        //important bit is to set Message type to 'chat', Google seems to ignore the default type
        Message msg = new Message(msgText, Message.Type.chat);
        chat.sendMessage(msg);
        return true;
    }

    public static void main(String args[]) {
    try {
        Communicator comm = new Communicator("username", "password");
        comm.sendMessage("", "");
        JOptionPane.showMessageDialog(null, "Close this when you want to quit");
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}
}

我有一个谷歌日历,我主要需要通过JavaAPI访问它。为此,我们有谷歌数据API,但问题是,日历是公开共享的,如果其中一个家伙想知道是否有人对日历进行了任何更改,他应该能够在不进行轮询的情况下自己获得通知。这有点像订阅,我无法实现。