2012年8月10日金曜日

Google APIs Calnedar & OAuth2.0 (v3/v2)

はじめに

Google APIs を使うコマンドラインを書いてみました。自分のイベントとか見たい方は適当に改良してください。:-)

** かぶりネタですが興味のある方は読んでください。**


Usage

USAGE
      gapicalendarlist.sh  v2|v3

RESET SETTINGS
      remove 'v2.gapi.token' or 'v3.gapi.token'

DEPENDENCIES
      lynx, curl


解説のようなメモのような

簡単に認証と処理内容を書いてみました

認証の種類

Version 2 : using ClientLogin
  • OAuth
  • AuthSub
  • ClientLogin
Version 3 : using OAuth -> Devices

知識の足りなさ等の諸事情で Devices の方法を選択してます。

Installed Applicatoins の方式で、https://accounts.google.com/o/oauth2/auth にリクエストするとHTMLコンテンツがかえって来ます。これはシェルであれこれするのが面倒だったのでやめました。
Devices方式で、https://accounts.google.com/o/oauth2/device/code にリクエストをすると、JSONがかえってpiar毎に改行されているのでシェルで扱いやすいのでこちらを選択しました

  • OAuth
    • Login
    • Web Server Applications
    • Client-size Applications
    • Installled Applications (Winwdows, iOS, Android, Blackberry)
    • Devices
    • Service Accounts

処理内容

*** Google のサイトを見た方がいいと思ったので割愛します。 ***

 [ shell ]                  [ google ]
    |                            |
    |                            |
    +----- get user token -----> +  URL 1 
    |                            |
    | <------ user token --------+
    |     and other params       |
    |          JSON              |
    |                            |
    +----+                       |
    |    | save token            |
    |<---+                       |
    |         [ lynx ]           |
    |            |               |
    +-- verify ->|               |
    |    URL     +-- Sign in --> |
    |            |               |
    |            |<- user code --+
    |            |     form      |
    |            |               |
    |            +- user code -->|
    |            |               |
    |            |<- allow acces-+
    |            |  confirm page |
    |            |               |
    |            +-allow submit->|
    |            |               |
    |            |<- result page-+
    |            |               |
    |                            |
    |                            |
    |                            |
    |                            |
    +------ get access token --->+ URL 2 
    |                            |
    | <------ access token ------+
    |       and other params     |
    |           JSON             |
    |                            |
    +----+                       |
    |    | save token            |
    |<---+                       |
    |                            |
    |                            |
    +----- get calendar list --->+  URL 3
    |                            |
    | <----- calendar list ------+
    |         JSON               |
    |                            |
    |                            |


URL 1 https://accounts.google.com/o/oauth2/device/code
URL 2 https://accounts.google.com/o/oauth2/token
URL 3 https://www.googleapis.com/calendar/v3/users/me/calendarList

Reference Link

Google OAuth 2.0

Google APIs Calendar



Source & Screen cast


ttyplay