Googleカレンダーをjavaで操作
引っ越し前のblogにも載せてましたが、諸事情によりこちらにも載せておきます。
最近、マッシュアップとかいうやつに興味を持って
少し勉強しようとしてます。
手始めに、Google API を使用してみたいと思ってます。
準備
Google カレンダーを作成
Google カレンダーを持ってない人は作成してください。
Google API(jarファイル)を取得
http://code.google.com/apis/gdata/client-java.html から Download the Java client library.」をクリックして「gdata-xxx.zip」を取得
プログラム作成
概要
Google APIは、gdataというAtom 1.0(またはRSS 2.0)に準じたフィードデータを使用してHTTP通信でやり取りするらしいです。
イベントの追加、変更、削除を行うアプリケーションでは一般的に、
「http://www.google.com/calendar/feeds /default/private/full」に対してリクエストを送信して行うらしいです。
・Eclipseでプロジェクトの作成とクラスパスの追加
・Eclipseのプロジェクトを作成(とりあえず、今回は、「googleCalApi」というJavaプロジェクトを作成)
・作成したプロジェクトにダウンロードした「gdata-xxx.zip」の中から以下のjarファイルを取得してクラスパスに追加
・gdata-calendar-2.0.jar
・gdata-core-1.0.jar
・gdata-client-1.0.jar
import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import com.google.gdata.client.Query; import com.google.gdata.client.calendar.CalendarService; import com.google.gdata.data.DateTime; import com.google.gdata.data.Entry; import com.google.gdata.data.Feed; import com.google.gdata.data.Person; import com.google.gdata.data.PlainTextConstruct; import com.google.gdata.data.calendar.CalendarEventEntry; import com.google.gdata.data.extensions.EventEntry; import com.google.gdata.data.extensions.When; import com.google.gdata.data.extensions.Where; import com.google.gdata.util.AuthenticationException; import com.google.gdata.util.ServiceException; /** * * GoogleCalendar APIで実行 * @version 1.0.0 */ public class GoogleCalMain { // Google アカウント private static String GOOGLE_ACCOUNT = "xxxxxxx@gmail.com"; // Google アカウントのパスワード private static String GOOGLE_PASSWORD = "xxxxxxxx"; // 送信URL private static String GOOGLE_CAL_URL = "http://www.google.com/calendar/feeds/default/private/full"; /** * Google カレンダーAPIを使用して、登録と参照 * @param args */ public static void main(String[] args) { GoogleCalMain main = new GoogleCalMain(); main.googleCalAddAndRef(); } /** * Google カレンダーAPIを使用して、 * 登録と登録したデータの参照を行います。 */ private void googleCalAddAndRef() { String title = "javaからの登録"; String place = "鹿児島"; String memo = "テスト"; try { URL postURL = new URL(GOOGLE_CAL_URL); // イベント登録クラス CalendarEventEntry myEntry = new CalendarEventEntry(); // スケジュールのタイトル myEntry.setTitle(new PlainTextConstruct(title)); // スケジュールの詳細 myEntry.setContent(new PlainTextConstruct(memo)); // 作成アプリ名 Person author = new Person("Mashup Sample Test", null, GOOGLE_ACCOUNT); myEntry.getAuthors().add(author); DateTime startTime = new DateTime(); startTime.setTzShift(9); startTime = DateTime.parseDateTime("2006-04-17T08:00:00"); DateTime endTime = new DateTime(); endTime.setTzShift(9); endTime = DateTime.parseDateTime("2006-04-17T17:00:00"); // 開始終了日時をWhen型オブジェクトに代入し、イベントクラスに追加 When eventTimes = new When(); eventTimes.setStartTime(startTime); eventTimes.setEndTime(endTime); myEntry.addTime(eventTimes); // 場所をWhere型オブジェクトに代入し、イベントクラスに追加 Where evLocation = new Where(); evLocation.setValueString(place); myEntry.addLocation(evLocation); // Google Calendarサービスに接続 CalendarService calService = new CalendarService("sample"); calService.setUserCredentials(GOOGLE_ACCOUNT, GOOGLE_PASSWORD); // スケジュールを追加する CalendarEventEntry insertEntry = calService.insert(postURL, myEntry); // 特定のスケジュールを操作するリクエストを取得 URL entryUrl = new URL(insertEntry.getSelfLink().getHref()); EventEntry retrievedEntry = calService.getEntry(entryUrl, EventEntry.class); // 特定のスケジュールを探す Query myQuery = new Query(postURL); myQuery.setFullTextQuery(title); Feed myResultsFeed = calService.query(myQuery, Feed.class); if (myResultsFeed.getEntries().size() > 0) { Entry firstMatchEntry = myResultsFeed.getEntries().get(0); System.out.println("Titie: " + firstMatchEntry.getTitle().getPlainText()); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (AuthenticationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } } }