リベルさんの戯言室

ゲームの攻略とか映画のネタバレとか。そのほかヌルヌル書いてます。リンクフリーです。※当ブログはPC版表示に最適化されています。

raspberry piでGoogleカレンダー取得スクリプトのセットアップ



www.raspberrypi.org

raspberry piGoogleカレンダー取得スクリプトのセットアップ

raspberrypiでGoogleカレンダーの取得ができるプログラムをkaraage氏が公開しています

karaage.hatenadiary.jp

上記Googleカレンダー取得スクリプトですが、若干手間取ったのでメモ
※2018年9月時点に確認。

発生したエラー

pyファイルダウンロード直後の以下コマンド実行でエラーが発生した

pi@raspberrypi:~ $ python get_gcal_schedule.py
Traceback (most recent call last):
  File "get_gcal_schedule.py", line 7, in <module>
    from oauth2client import client
ImportError: No module named oauth2client

oauth2clientがないため実行に失敗したようです。

oauth2clientのインストール

pi@raspberrypi:~ $ sudo pip install --upgrade oauth2client --ignore-installed six

再実行

pi@raspberrypi:~ $ python get_gcal_schedule.py
/usr/local/lib/python2.7/dist-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access /home/pi/.credentials/calendar-python-quickstart.json: No such file or directory
  warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Traceback (most recent call last):
  File "get_gcal_schedule.py", line 91, in <module>
    get_schedule()
  File "get_gcal_schedule.py", line 60, in get_schedule
    credentials = get_credentials()
  File "get_gcal_schedule.py", line 45, in get_credentials
    flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 2135, in flow_from_clientsecrets
    cache=cache)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/clientsecrets.py", line 165, in loadfile
    return _loadfile(filename)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/clientsecrets.py", line 125, in _loadfile
    exc.strerror, exc.errno)
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secret.json', 'No such file or directory', 2)

calendar-python-quickstart.jsonがないためエラーが発生してしまいました。。。

calendar-python-quickstart.jsonの入手

ここが一番詰まりました。結果としては、以下サイトのSTEP3の実行を行うことで自動的に生成されました
dev.classmethod.jp

STEP3項のソースを「quickstart.py」としてルートディレクトリに格納し、実行しましょう

pi@raspberrypi:~ $ python quickstart.py
/usr/local/lib/python2.7/dist-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access /home/pi/.credentials/calendar-python-quickstart.json: No such file or directory
  warnings.warn(_MISSING_FILE_MESSAGE.format(filename))

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcalendar.readonly&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&response_type=code&client_id=361305561604-fvr9cg1fmun1elgn2a4ff2joomgalqes.apps.googleusercontent.com&access_type=offline

If your browser is on a different machine then exit and re-run this
application with the command-line parameter

  --noauth_local_webserver

このタイミングでブラウザが起動します。ログインして認証しましょう。

以後get_gcal_schedule.pyが正常に動作するようになります。

※quickstart.pyのソースがget_gcal_schedule.pyに組み込まれているので、うまく行きそうな気はするんですが、原因は謎です。