TaskSphere:Google Calendar APIを使ってイベントを追加しよう!

AI

こんにちは!今回は、PythonとGoogle Calendar APIを使って、Googleカレンダーに新しいイベントを追加するプログラムを解説します。

事前準備

サービスアカウントの作成と設定

Google Calendar APIを使うには、サービスアカウントの作成と設定が必要です。手順は以下の通りです。

  1. Google Cloud Platformにログインし、プロジェクトを選択(または新規作成)
  2. APIとサービス>認証情報から、「認証情報を作成」>「サービスアカウント」を選択
  3. サービスアカウント名を入力し、「作成」
  4. アクセスするユーザーを選択し、「完了」
  5. 作成したサービスアカウントの「アクション」>「キーを作成」>「JSON」を選択し、キーをダウンロード
  6. ダウンロードしたJSONファイルをkey.jsonにリネームし、プロジェクトのディレクトリに配置

必要なライブラリのインストール

!pip install google-auth google-api-python-client art termcolor

コードの解説

環境変数の設定


GOOGLE_APPLICATION_CREDENTIALS=key.json
CALENDAR_ID=XXXXXXXXXXXXXX@group.calendar.google.com
  • GOOGLE_APPLICATION_CREDENTIALS:サービスアカウントのキーファイル(key.json)のパス
  • CALENDAR_ID:イベントを追加するカレンダーのID

ライブラリのインポートとスクリプト名の表示

import os
from google.oauth2 import service_account
from googleapiclient.discovery import build
from art import *
from termcolor import colored

script_name = os.path.basename(__file__)
tprint(script_name)
  • 必要なライブラリをインポート
  • arttermcolorを使ってスクリプト名を装飾して表示

認証情報の取得とサービスオブジェクトの生成

SCOPES = ['https://www.googleapis.com/auth/calendar']
service_account_file = os.environ['GOOGLE_APPLICATION_CREDENTIALS']

creds = service_account.Credentials.from_service_account_file(service_account_file, scopes=SCOPES)
service = build('calendar', 'v3', credentials=creds)
  • SCOPESにGoogle Calendar APIのスコープを設定
  • 環境変数からサービスアカウントのキーファイルのパスを取得
  • サービスアカウントの認証情報を取得
  • Google Calendar APIのサービスオブジェクトを生成

イベントデータの作成と追加

calendar_id = os.environ['CALENDAR_ID']

event = {
    'summary': 'Google 2019',
    'location': '800 Howard ',
    'description': 'A chance to hear more about Google',
    'start': {
        'dateTime': '2024-05-25T00:00:00-07:00',
    },
    'end': {
        'dateTime': '2024-05-25T01:00:00-07:00',
    },
}

created_event = service.events().insert(calendarId=calendar_id, body=event).execute()
  • 環境変数からカレンダーIDを取得
  • 追加するイベントのデータを辞書形式で作成
    • summary:イベントのタイトル
    • location:イベントの場所
    • description:イベントの説明
    • start:イベントの開始日時
    • end:イベントの終了日時
  • service.events().insert()でイベントを追加

追加したイベントの情報を表示

print(colored('Event details:', 'green'))
print(colored('Summary: ', 'yellow') + colored(created_event['summary'], 'cyan'))
print(colored('Location: ', 'yellow') + colored(created_event['location'], 'cyan'))
print(colored('Description: ', 'yellow') + colored(created_event['description'], 'cyan'))
print(colored('Start: ', 'yellow') + colored(created_event['start']['dateTime'], 'cyan'))
print(colored('End: ', 'yellow') + colored(created_event['end']['dateTime'], 'cyan'))
print(colored('Event URL: ', 'yellow') + colored(created_event['htmlLink'], 'cyan'))
  • termcolorを使って、追加したイベントの情報を色付きで表示

以上で、Google Calendar APIを使ってイベントを追加するPythonプログラムの解説は終了です。APIを使えば、Googleカレンダーをプログラムから自在に操作できます。ぜひ活用してみてください!

リポジトリ

GitHub - Sunwood-ai-labs/TaskSphere
Contribute to Sunwood-ai-labs/TaskSphere development by creating an account on GitHub.

参考サイト

API を使用して Projects を管理する - GitHub Docs
GraphQL API を使用して、プロジェクトを自動化できます。
GitHub GraphQL APIを利用し、複数リポジトリが紐づくGitHub ProjectsのIssueリストを取得する - いいものを広め隊
はじめに 今回想定するGitHub Projects GitHub GraphQL APIを使うまでの準備 1. アクセストークンの登録 2. Issueを取得したいGitHub Projectsのidを取得する 今回はPythonで実行します GitHub Projectsに紐づくIssue一覧を取得する 特定のステ...
個人用アクセス トークンを管理する - GitHub Docs
コマンド ラインまたは API を使用して GitHub への認証を行うときに、パスワードの代わりに personal access token を使用することができます。

コメント

タイトルとURLをコピーしました