LoginSignup
44
44

More than 3 years have passed since last update.

Google Calendar APIを使用してみる #1

Last updated at Posted at 2020-02-25

Google Calendar API をちょろっとだけ触ってみたくなった

いろんな分野に目移りしてちょろっと触ってみたくなる悪い癖なんですが
クラウドソーシングの副業を探していたら、Gooogle Calendarに予定を突っ込む/更新する案件があり
なぜかそれきっかけで無性にAPIを呼んでみたくなったので、試してみました。
※ちなみに案件は受けていませんw

シリーズ

Google Calendar APIを使用してみる #2
Google Calendar APIを使用してみる #3

環境

IDE:VisualStudio2019
アプリケーション:コンソールアプリ
フレームワーク:.NET Core 3.1

Google Calendar API 入門

公式に入門がありました。
Get Started with the Calendar API

どうやらAPIを使用するには、認証情報を作成する必要があるようです。
以下3つの方法があるとのことです。
image.png

OAuthクライアントIDを用いる方法では、すぐに認証情報が作成できるのですが、「oauth 同意画面」の設定を行わないとプライベートデータへのアクセスが100回までと制限されるそうで
image.png
また、「oauth 同意画面」の設定が手間そうだったので、「サービス アカウント」を用いる方法を試してみたいと思います。

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

以下に入力を行い作成。
image.png

出来上がったサービスアカウントのリンクをクリック
image.png

キーを作成をクリック
image.png

JSONを選択
image.png

ここでダウンロードしたJsonの中身の情報を用いてGoogleAPIを呼び出します。

クライアントライブラリ使用して予定を取得

Nugetからカレンダー用のDLLをダウンロードしてきます。
image.png

次に作成したサービスアカウントに操作させたいカレンダーを公開します。
対象のカレンダーの設定から特定のユーザーとの共有にて追加します。
image.png
メールアドレスのところに、サービスアカウントのメールアドレスを入れます。
※ダウンロードしたJSONのclient_emailにもあります。

サンプルコードの実装
以下.Netのサンプルが公式にありましたので、そちらを参考にしてみます。
.Net Sample

とりあえずこぴって、環境に依存するところは変更。
また、サービスアカウントで認証する必要があるので以下を参考にして、credentialを作成するところは書き換えた。
Google Calendar API ServiceAccountCredential

using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Threading;

namespace GoogleAPITest
{
    public class Program
    {
        // If modifying these scopes, delete your previously saved credentials
        // at ~/.credentials/calendar-dotnet-quickstart.json
        private static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
        private static string ApplicationName = "Google Calendar API .NET Quickstart";

        public static void Main(string[] args)
        {
            var jObject = JObject.Parse(File.ReadAllText(
                @"C:\job\TestProject\GoogleAPITest\testproject-269217-813bf9be17a5.json"));
            var serviceAccountEmail = jObject["client_email"].ToString();
            var certificate = jObject["private_key"].ToString();

            var credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = new[] { CalendarService.Scope.Calendar }
            }.FromPrivateKey(certificate));

            // Create Google Calendar API service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            // おそらく不必要
            // Define parameters of request.
            //var request = service.Events.List("primary");
            //request.TimeMin = DateTime.Now;
            //request.ShowDeleted = false;
            //request.SingleEvents = true;
            //request.MaxResults = 10;
            //request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;

            // ここで第2引数にサービスアカウントに公開したカレンダーIDを指定する
            var request = new EventsResource.ListRequest(service, "公開したカレンダーのカレンダーID");

            // List events.
            var events = request.Execute();
            Console.WriteLine("Upcoming events:");
            if (events.Items != null && events.Items.Count > 0)
            {
                foreach (var eventItem in events.Items)
                {
                    string when = eventItem.Start.DateTime.ToString();
                    if (String.IsNullOrEmpty(when))
                    {
                        when = eventItem.Start.Date;
                    }

                    Console.WriteLine("{0} start:({1}) end:({2})", eventItem.Summary, when, eventItem.End.DateTime.ToString());
                }
            }
            else
            {
                Console.WriteLine("No upcoming events found.");
            }

            Console.Read();
        }
    }
}

公開したカレンダーの直近
image.png

実行結果
image.png

正常に取得できているようです。(昔のイベントは無視で・・・)

今回はここまでにします。
次回はカレンダー更新をやってみようと思います。
次回

44
44
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
44
44