简体   繁体   English

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

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

Can anyone tell me how to connect to Google Calendar server using the Smack API, if i can do that.谁能告诉我如何使用 Smack API 连接到 Google 日历服务器,如果我可以的话。

What do you mean by connecting to Google Calendar using Smack? 使用Smack连接到Google日历是什么意思? Smack will let you connect to an XMPP server, which is an instant messaging means. Smack可以让您连接到XMPP服务器,这是一种即时消息传递方式。 If you want something similar to http://clockwerx.blogspot.com/2008/10/interacting-with-google-calendar-via.html then there are two sides: implement the bot and then connect to it and talk to it. 如果您想要类似于http://clockwerx.blogspot.com/2008/10/interacting-with-google-calendar-via.html的内容,则有两个方面:实现机器人,然后连接到该机器人并与之对话。 But generally speaking, here's a code sample that will let you connect to GTalk: 但总的来说,这是一个代码示例,可让您连接到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();
    }
}
}

I doubt the problem still persists after that long time.我怀疑经过这么长时间后问题仍然存在。 But for future users searching for an answer I will provide it.但对于未来寻找答案的用户,我会提供它。

Smack API is used for XMPP messaging. Smack API 用于 XMPP 消息传递。 It is unlikely you get calendar access with that.您不太可能通过它获得日历访问权限。

Most (cloud based) calendars do work based on a specialization of WebDAV which is called CalDAV .大多数(基于云的)日历都基于WebDAV的专门化工作,称为CalDAV A suitable client library is caldav4j .合适的客户端库是caldav4j

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM