鶏頭のプログラム

Ruby, Javascript, C言語, SQLなどのプログラミング

RubyでGoogleカレンダーの情報を引き抜いてみる

5,6年前に作ったGoogleカレンダーと会社のカレンダーを同期するシステムが
突然動かなくなったので修正しました。
理由は簡単で
Google Calendar Api V2がサポート切れになったから!

私は最近このプログラムを使ってなかったのでまったく困ってなかったのですが
他の人が結構ヘビーに使っていた人がいたので
重い腰をあげてGoogle Calendar API v3 に対応しました。

もともとこのコードはJavaで書いていたのですが、せっかくなのでRubyで書きなおしを
行いました。

そこそこ面倒だったので、手順を記載します。

基本は下記サイトの手順で行いました。


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

しかしながら
下記のコマンドを実行を実行した際に問題が発生しました。

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

サイト内ではこのコマンドを実行するとブラウザが立ち上がり
OAuthの認証の承認をすると ~/.google-api.yaml がダウンロードされるという旨が記載されているが
一向にダウンロードされません。
理由は簡単で、Google ChromeおよびFireFoxでは.google-api.yamlファイルがダウンロード
されるのですがなぜかIEだとされないというよくわからない不具合のせい。

なので、IEでも問題ないようにコードを書きました。

# coding: utf-8

require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
require 'json'
require 'yaml'

client = Google::APIClient.new(
  :application_name => 'Example Ruby application',
  :application_version => '1.0.0'
)
plus = client.discovered_api('plus')

flow = Google::APIClient::InstalledAppFlow.new(
  :client_id => 'クライアントID',
  :client_secret => 'クライアントシークレット',
  :scope => ['https://www.googleapis.com/auth/calendar']
)

client.authorization = flow.authorize
File.open("./.google-api.yaml", "w"){|f|
	x = {
		"mechanism" => "oauth_2",
		"scope" => "https://www.googleapis.com/auth/calendar",
		"client_id" => client_secrets.client_id,
		"client_secret" => client_secrets.client_secret,
		"access_token" => client.authorization.access_token,
		"refresh_token" => client.authorization.refresh_token
	}
	f.puts(x.to_yaml)
}

上記コードを実行すると.google-api.yamlファイルが実行ディレクトリに作成されます。

あとはgoogleカレンダーからデータを取得します。

oauth_yaml = YAML.load_file('./.google-api.yaml')
client = Google::APIClient.
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"]
al = client.discovered_api('calendar', 'v3')

# 時間を格納 向こう一か月分
today = Date.today
time_min = Time.utc(today.year,today.month, today.day, 0).iso8601
time_max = Time.utc(onemonth.year, onemonth.month, onemonth.day, 0).iso8601

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

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

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


result.data.items.each do |event|
        p event.start.dateTime # 開始時間
        p event.end,dateTime # 終了時間
        p event.summary # タイトル
        p event.location # 場所
end

こんな感じになります。