Google Claender APIの使い方をメモ
基本的にはSpread Sheetと同様
GCP側の設定
まずGCPでプロジェクトを作ったら、右のメニューバーから、APIとサービス=>有効なAPIとサービスを選択

APIとサービスの有効化を選択

calenderで検索するとGoogle Calender APIが出てくるので選択し有効化

サービスアカウント管理を選択

サービスアカウントを作成を選択

情報を適当(任意の値)に入力し作成

入力完了した認証情報が、作成されるので選択

キー=>鍵を追加=>新しい鍵を作成を選択

JSONで選択すると自動でcrendentialファイルがダウンロードされる
※1回しか発行されないので管理に注意(再作成は可能だが別物になる)
Calender側の設定
Calenderの設定メニューを選択

予定のアクセス権限からユーザーを追加を選択

メールアドレスに先ほどのサービスアカウントを入力
権限は変更及び共有の管理権限を選択

Google APIを利用
Pythonで記述。Google APIで認証しながら進めていく。
必要なライブラリをインストール
pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib
カレンダー取得コード例
import datetime
from googleapiclient.discovery import build
from google.oauth2 import service_account
#設定値
SCOPES = ['https://www.googleapis.com/auth/calendar']
calendar_id = 'hogehoge'
conf_path = 'fugafuga'
def main():
#認証
creds =service_account.Credentials.from_service_account_file(conf_path, scopes=SCOPES)
service = build('calendar', 'v3', credentials=creds)
now = datetime.datetime.utcnow().isoformat() + 'Z'
#直近10件を取得
events_list = service.events().list(
calendarId=calendar_id,
timeMin=now,
maxResults=10,
singleEvents=True,
orderBy='startTime'
).execute()
if __name__ == '__main__':
main()
結果はjsonで出力される
コメント