Google Calendar API
JavaからGoogle Calendar APIをさわってみたいと思い、調べています。
Googleのドキュメントはこのあたり。
- Google Data APIs Client Libraries
- API Reference Guide - Google Calendar APIs and Tools
- API Developer's Guide: Java - Google Calendar APIs and Tools
- Google Data APIs Client Library (1.31.0) (Javadoc)
サンプルを実行してみる
GData Java Client Libraryから、Javaのライブラリとサンプルがダウンロードできます。一通りのGoogleサービスのAPIを使うサンプルが入っているでの実行してみます。
カレンダーのサンプルを実行するためには、java\build-samples\build.propertiesをエディタで開いて、アカウント情報を編集して、
# Authentication credentials to use for the samples # EDIT-THIS: Add your own Google credentials to run the samples. sample.credentials.username=アカウント@gmail.com sample.credentials.password=パスワード
antを実行します。
ant -f build-samples.xml sample.calendar.run
これで、カレンダーに登録されているイベントを出力するEventFeedDemo、自分のカレンダー一覧を出力するCalenarFeedDemo、カレンダーのAccess Control Listを出力するACLFeedDemoが実行されます。
Eclipse Plugin
Eclipse pluginもあります。このpluginは特定のサービスにアクセスするために必要なライブラリを設定したプロジェクトを作成してくれます。
gdata-java-client-eclipse-plugin
このPluginで作成したプロジェクトでCalendarにアクセスするプログラムを作成したところ↓のようなエラーが出ました。
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/Maps at com.google.gdata.wireformats.AltRegistry.(AltRegistry.java:118) at com.google.gdata.wireformats.AltRegistry. (AltRegistry.java:100) at com.google.gdata.client.Service. (Service.java:512)
これを回避するには、GData Java Client Libraryに含まれるgoogle-collect-1.0-rc1.jarをクラスパスに追加します。
EventFeedにアクセスしてみる
まずは、Google Calendarにアクセスして公開カレンダーのエントリを取得するサンプルです。IT勉強会カレンダーを取得してコンソールにタイトルを取得します。
クラスパスには、以下のjarを追加します。
- gdata-client-1.0.jar
- gdata-core1.0.jar
- gdata-calendar-2.0.jar
- google-collect-1.0-rc1.jar
package com.azuki3; import java.io.IOException; import java.net.URL; import com.google.gdata.client.calendar.CalendarService; import com.google.gdata.data.calendar.CalendarEventEntry; import com.google.gdata.data.calendar.CalendarEventFeed; import com.google.gdata.util.ServiceException; public class CalendarSample { public static void main(String[] arg) { CalendarService service = new CalendarService("azuki3.com-CalendarTest-1"); try { URL eventFeedUrl = new URL( "http://www.google.com/calendar/feeds/fvijvohm91uifvd9hratehf65k%40group.calendar.google.com/public/basic"); CalendarEventFeed resultFeed = service.getFeed(eventFeedUrl, CalendarEventFeed.class); System.out.println("IT勉強会カレンダー " + resultFeed.getEntries().size() + " entries:"); for (int i = 0; i < resultFeed.getEntries().size(); i++) { CalendarEventEntry entry = resultFeed.getEntries().get(i); System.out.println(entry.getTitle().getPlainText()); } } catch (IOException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } } }
以下のように出力されました。
IT勉強会カレンダー 25 entries: 【延期】[大阪]SSL技術セミナー:SSLの仕組から暗号アルゴリズム2010年問題まで 【中止】[北海道/札幌]Tech Fielders セミナー 【中止】[福岡]Tech Fielders セミナー 【中止】[大阪]Tech Fielders セミナー ...
25件Feedが取得できました。並び順はエントリーの更新日時順のようです。
とりあえず今日はここまでで。