質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Google カレンダー

Google カレンダーは、Google社が提供する無料のスケジュール管理ツールです。パソコンやスマートフォン、タブレットなどからアクセスし、スケジュールの追加・変更が可能。Googleアカウントがあれば誰でも使用できます。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Q&A

0回答

2155閲覧

rubyを使用した、google calendar取得について

kamekame0505

総合スコア12

Google カレンダー

Google カレンダーは、Google社が提供する無料のスケジュール管理ツールです。パソコンやスマートフォン、タブレットなどからアクセスし、スケジュールの追加・変更が可能。Googleアカウントがあれば誰でも使用できます。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

0グッド

0クリップ

投稿2020/05/24 15:00

前提・実現したいこと

raspberrypiにてsmashingを使用した、ダッシュボードを作成しております。

google calendarを、rubyから取得をしたく、API関連の設定を行い
ruby quickstartのサンプルコードの動作まで確認ができました。

ダッシュボードの作成にあたり、ruby quickstartのコードを元に情報を検索し、
過去にダッシュボードで使用するために公開されていたコードを真似て作りましたが、
ruby初心者のため、下記の内容のエラーで躓いています。
エラーの内容から、'data'が定義されておらず、カレンダーの例外が発生との内容ですが、どこの部分に'data'の定義が漏れているか分かりません。
また、カレンダーの例外発生という内容も理解できませんでした。

コンパイルは成功しており、smashing startでプログラムとしては動作し、
ダッシュボードにアクセスできますがカレンダーにアクセスできず情報が取得出来ない状態です。

発生している問題・エラーメッセージ

scheduler caught exception: undefined method `data' for #<Google::Apis::CalendarV3::Events:0x0000562bb8ff1f88>

該当のソースコード

ruby

1※一部箇所、ファイル名を隠すため***と表記しています。 2require "google/apis/calendar_v3" 3require "googleauth" 4require "googleauth/stores/file_token_store" 5require "date" 6require "fileutils" 7require "rufus-scheduler" 8 9OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze 10APPLICATION_NAME = "Google Calendar API Ruby Quickstart".freeze 11CREDENTIALS_PATH = "***.json".freeze 12# The file token.yaml stores the user's access and refresh tokens, and is 13# created automatically when the authorization flow completes for the first 14# time. 15TOKEN_PATH = "***.yaml".freeze 16SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR 17 18## 19# Ensure valid credentials, either by restoring from the saved credentials 20# files or intitiating an OAuth2 authorization. If authorization is required, 21# the user's default browser will be launched to approve the request. 22# 23# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials 24def authorize 25client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH 26token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH 27authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store 28user_id = "default" 29credentials = authorizer.get_credentials user_id 30if credentials.nil? 31url = authorizer.get_authorization_url base_url: OOB_URI 32puts "Open the following URL in the browser and enter the " \ 33"resulting code after authorization:\n" + url 34code = gets 35credentials = authorizer.get_and_store_credentials_from_code( 36user_id: user_id, code: code, base_url: OOB_URI 37) 38end 39credentials 40end 41 42# Start the scheduler 43SCHEDULER.every '15m', :first_in => 4 do |event| 44 45# Initialize the API 46service = Google::Apis::CalendarV3::CalendarService.new 47service.client_options.application_name = APPLICATION_NAME 48service.authorization = authorize 49 50# Request a token for our service account 51service.authorization.fetch_access_token! 52 53# Fetch the next 10 events for the user 54calendar_id = "primary" 55response = service.list_events(calendar_id, 56max_results: 10, 57single_events: true, 58order_by: "startTime", 59time_min: DateTime.now.rfc3339) 60 61response.items.each do |event| 62 63start = event.start.date || event.start.date_time 64 65send_event('google_calendar', { events: response.data }) 66 67 end 68end

###試したこと
Google::Apis::CalendarV3::Events:0x0000562bb8ff1f88の内容について
できる限り調べましたが、力が及ばず自分では解決不可能でした。

補足情報(FW/ツールのバージョンなど)

コードの参考は下記のgitHUBから参照しました。
https://gist.github.com/jmb/33ae3a33d6e8dbffd102
https://github.com/orangain/mydashboard/blob/master/jobs/calendar.rb

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

taichi730

2020/05/25 01:29

参考にしたコードが対象にしている、Gem のバージョンが古いのでは? 公式ドキュメントを参考にしたほうが良いかと。
kamekame0505

2020/05/27 15:09

投稿ありがとうございます。参考にしたコードはgoogle-api-client 0.8.6当たりのコードになります。 そのため、API認証に必要なコードのみ現行のruyb quickstartから拝借した形です。 指摘頂いた、内容を確認してみます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問