2009年8月26日水曜日

Google Calendar APIを使ってみた。

RESTfulでJSON形式を利用してみた。
これはAndroidで使える。

alt=jsonを付けないとXML形式(デフォールト)になる。

gsessionidを取得するのは最初一回やる必要がある。登録、変更処理で必要。


1.認証
[URL]
POST https://www.google.com/accounts/ClientLogin

[Parameters]
Email=BABUKUMATEST@gmail.com
Passwd=XXXXXXXXX
source=BABUKUMAAPP
service=cl

[Http Request]
POST /accounts/ClientLogin HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: http4e/1.5.4
Host: www.google.com
Content-Length: 73

Email=BABUKUMATEST@gmail.com&Passwd=XXXXXXXXX&source=BABUKUMAAPP&service=cl&

[Http Response]
HTTP/1.1 200 OK
Content-Type: text/plain
Cache-control: no-cache, no-store
Pragma: no-cache
Expires: Mon, 01-Jan-1990 00:00:00 GMT
Date: Wed, 26 Aug 2009 04:48:07 GMT
X-Content-Type-Options: nosniff
Content-Length: 563
Server: GFE/2.0

SID=BABUKUMABABUKUMABABUKUMABABUKUMABABUKUMABABUKUMA
LSID=BABUKUMABABUKUMABABUKUMABABUKUMABABUKUMABABUKUMA
Auth=「ごごにGoogleLogin auth Tokenが」


2. カレンダーリスト取得
[URL]
GET http://www.google.com/calendar/feeds/default/allcalendars/full
[Headers]
Content-Type=application/x-www-form-urlencoded
Authorization: GoogleLogin auth=「GoogleLogin auth Token」

[Parameters]
alt=json

[Http Request]
GET /calendar/feeds/default/allcalendars/full?alt=json& HTTP/1.1
Authorization: GoogleLogin auth=「GoogleLogin auth Token」
Content-Type: application/x-www-form-urlencoded
User-Agent: http4e/1.5.4
Host: www.google.com

GET /calendar/feeds/default/allcalendars/full?alt=json&&gsessionid=BABUKUMABAB HTTP/1.1
Authorization: GoogleLogin auth=「GoogleLogin auth Token」
Content-Type: application/x-www-form-urlencoded
User-Agent: http4e/1.5.4
Host: www.google.com
Cookie: $Version=0; S=calendar=BABUKUMABABUKUMA

[Http Response]
HTTP/1.1 200 OK
Expires: Wed, 26 Aug 2009 05:20:27 GMT
Date: Wed, 26 Aug 2009 05:20:27 GMT
Content-Type: application/json; charset=UTF-8
Cache-Control: private, max-age=0, must-revalidate, no-transform
Vary: Accept, X-GData-Authorization, GData-Version
GData-Version: 1.0
Last-Modified: Wed, 26 Aug 2009 05:20:27 GMT
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
Server: GFE/2.0

{"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$gCal":"http://schemas.google.com/gCal/2005","xmlns$gd":"http://schemas.google.com/g/2005","id":{"$t":"http://www.google.com/calendar/feeds/default/allcalendars/full"},"updated":{"$t":"2009-08-26T05:20:27.500Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","term":"http://schemas.google.com/gCal/2005#calendarmeta"}],".....................................................................}


3. gsessionid取得

一回登録処理をするとレスポンスのLocation部分にgsessionidが入ってる。これを保存する。


4. 予定登録
[URL]
POST http://www.google.com/calendar/feeds/「User_ID」/private/full?alt=json&gsessionid=「取得したgsessionid」

[Headers]
Content-Type=application/atom+xml
Authorization: GoogleLogin auth=「GoogleLogin auth Token」

[Body]
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:gd='http://schemas.google.com/g/2005'>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/g/2005#event'></category>
<title type='text'>Babukuma Test 2</title>
<content type='text'>日本語 Test Test</content>
<gd:transparency
value='http://schemas.google.com/g/2005#event.opaque'>
</gd:transparency>
<gd:eventStatus
value='http://schemas.google.com/g/2005#event.confirmed'>
</gd:eventStatus>
<gd:where valueString='神田'></gd:where>
<gd:when startTime='2009-08-26T15:00:00.000Z'
endTime='2009-08-26T23:00:00.000Z'></gd:when>
</entry>

[Http Request]
HTTP/1.1 201 Created
Expires: Wed, 26 Aug 2009 05:37:13 GMT
Date: Wed, 26 Aug 2009 05:37:13 GMT
Content-Type: application/json; charset=UTF-8
Cache-Control: private, max-age=0, must-revalidate, no-transform
Vary: Accept, X-GData-Authorization, GData-Version
GData-Version: 1.0
Location: http://www.google.com/calendar/feeds/「User_ID」/private/full/babukumababukuma/63386948233
Content-Location: http://www.google.com/calendar/feeds/「User_ID」/private/full/babukumababukuma/63386948233
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
Server: GFE/2.0

{"version":"1.0","encoding":"UTF-8","entry":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$gml":"http://www.opengis.net/gml","xmlns$georss":"http://www.georss.org/georss","xmlns$batch":"http://schemas.google.com/gdata/batch","xmlns$gCal":"http://schemas.google.com/gCal/2005","xmlns$gd":"http://schemas.google.com/g/2005","id":.....................................................................}



5. 予定取得
[URL]
GET http://www.google.com/calendar/feeds/「User_ID」/private/full?alt=json

[Headers]
Content-Type=application/x-www-form-urlencoded
Authorization: GoogleLogin auth=「GoogleLogin auth Token」

[Parameters]
alt=json
max-results=15
singleevents=true
sortorder=ascending
futureevents=true

[Http Request]
GET /calendar/feeds/「User_ID」/private/full?alt=json&max-results=15&singleevents=true&sortorder=ascending&futureevents=true& HTTP/1.1
Authorization: GoogleLogin auth=「GoogleLogin auth Token」
Content-Type: application/x-www-form-urlencoded
User-Agent: http4e/1.5.4
Host: www.google.com

GET /calendar/feeds/「User_ID」/private/full?alt=json&max-results=15&singleevents=true&sortorder=ascending&futureevents=true&&gsessionid=「取得したgsessionid」 HTTP/1.1
Authorization: GoogleLogin auth=「GoogleLogin auth Token」
Content-Type: application/x-www-form-urlencoded
User-Agent: http4e/1.5.4
Host: www.google.com
Cookie: $Version=0; S=calendar=babukumababukuma


[Http Response]
HTTP/1.1 200 OK
Expires: Wed, 26 Aug 2009 05:34:39 GMT
Date: Wed, 26 Aug 2009 05:34:39 GMT
Content-Type: application/json; charset=UTF-8
Cache-Control: private, max-age=0, must-revalidate, no-transform
Vary: Accept, X-GData-Authorization, GData-Version
GData-Version: 1.0
Last-Modified: Wed, 26 Aug 2009 05:30:27 GMT
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
Server: GFE/2.0

{"version":"1.0","encoding":"UTF-8","feed":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$openSearch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$gml":"http://www.opengis.net/gml","xmlns$georss":"http://www.georss.org/georss","xmlns$batch":"http://schemas.google.com/gdata/batch","xmlns$gC.....................................................................}