簡體   English   中英

在 JAVA 中使用 Smack API 連接到 Google 日歷

[英]Connect to Google Calendar using Smack API in JAVA

誰能告訴我如何使用 Smack API 連接到 Google 日歷服務器,如果我可以的話。

使用Smack連接到Google日歷是什么意思? Smack可以讓您連接到XMPP服務器,這是一種即時消息傳遞方式。 如果您想要類似於http://clockwerx.blogspot.com/2008/10/interacting-with-google-calendar-via.html的內容,則有兩個方面:實現機器人,然后連接到該機器人並與之對話。 但總的來說,這是一個代碼示例,可讓您連接到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();
    }
}
}

我懷疑經過這么長時間后問題仍然存在。 但對於未來尋找答案的用戶,我會提供它。

Smack API 用於 XMPP 消息傳遞。 您不太可能通過它獲得日歷訪問權限。

大多數(基於雲的)日歷都基於WebDAV的專門化工作,稱為CalDAV 合適的客戶端庫是caldav4j

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM