LoginSignup
130
135

More than 5 years have passed since last update.

rubyにてgoogleカレンダーの情報を取得する

Last updated at Posted at 2014-01-02

どうも、年末年始は[ハンターハンター読む]→[rubyいじる]→[餅食べる]→[ハンターハンター読む]→[rubyいじる]→[餅食べる]をローテーションしている千葉です。

今日は、google-api-ruby-clientを使用して、カレンダーの情報を表示してみたいと思います。

目的

googleカレンダーの情報を取得する。
私は、出勤・退勤をカレンダー登録してるので、それをいい感じに出力して、使うため。
ここでは、登録内容を標準出力することを目的とします。

環境

  • OS X 10.9.1
  • ruby 2.0.0p247 (2013-06-27 revision 41674)
  • gem 2.2.0

手順

  1. google-api-ruby-clientのインストール
  2. google APIの有効とAPIキーの取得
  3. OAuth認証を行い、アクセストークンを取得する
  4. いざ、rubyを実行

手順詳細

google-api-ruby-clientのインストール

$ sudo gem install google-api-client -v 0.6.4
$ gem install jwt -v 0.1.5

google APIの有効とAPIキーの取得

カレンダーAPIを使用できるように許可

スクリーンショット 2014-01-02 5.34.27.png

  • OAuthで使用するクライアントIDの取得

Credential > CREATE NEW CLIENT IDを選択

スクリーンショット 2014-01-01 22.51.47.png

Installed applicationを選択

スクリーンショット 2014-01-01 23.17.38.png

生成された「Client ID」と「Client secret」はOAuth認証で使用するためメモしておく

  • アプリケーションの登録

Consent screenより「EMAIL ADDRESS」、「PRODUCT NAME」を設定

OAuth認証を行い、アクセストークンを取得する

先ほど取得した「Client ID」と「Client secret」を使い認証を行う。

$ google-api oauth-2-login --client-id="<your client id>" --client-secret="<your client secret>" --scope="https://www.googleapis.com/auth/calendar"

ブラウザが開くので「承認」を選択する。

スクリーンショット 2014-01-02 0.01.17.png

以下のファイルが作成されていることを確認する。

ls ~/.google-api.yaml

このファイルにAPIアクセスに必要な情報が入っている。

いざ、rubyの実行

サンプルコードとして以下のっけておきます。
他にもAPIが色々あるので、色々できそう。
https://developers.google.com/apis-explorer/#p/calendar/v3/

サンプルコード

calendar.rb
# Initialize the client & Google+ API
require 'google/api_client'
require "yaml"
require "time"

# Initialize OAuth 2.0 client
# authorization
oauth_yaml = YAML.load_file('.google-api.yaml')
client = Google::APIClient.new(:application_name => '')
client.authorization.client_id = oauth_yaml["client_id"]
client.authorization.client_secret = oauth_yaml["client_secret"]
client.authorization.scope = oauth_yaml["scope"]
client.authorization.refresh_token = oauth_yaml["refresh_token"]
client.authorization.access_token = oauth_yaml["access_token"]

cal = client.discovered_api('calendar', 'v3')

# イベント取得月の確認
printf("カレンダーを表示する年(20XX):")
year = gets.strip.to_i
printf("カレンダーを表示する月(1-12):")
month = gets.strip.to_i

# 時間を格納
time_min = Time.utc(year, month, 1, 0).iso8601
time_max = Time.utc(year, month, 31, 0).iso8601

# イベントの取得
params = {'calendarId' => 'primary',
          'orderBy' => 'startTime',
          'timeMax' => time_max,
          'timeMin' => time_min,
          'singleEvents' => 'True'}

result = client.execute(:api_method => cal.events.list,
                        :parameters => params)

# イベントの格納
events = []
result.data.items.each do |item|
        events << item
end

# 出力
events.each do |event|
    printf("%s,%s\n",event.start.date,event.summary)
end

課題

  • イベントが日付順に表示されないので、悩み中【解決】コード取り込み済み

参考URL

130
135
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
130
135