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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

Q&A

解決済

1回答

4082閲覧

LaravelでGoogleカレンダーの予定作成時に「ゲストを追加」したい(Google Calendar API Service Account Error)

yuuta_s

総合スコア0

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

Google

Googleは、アメリカ合衆国に位置する、インターネット関連のサービスや製品を提供している企業です。検索エンジンからアプリケーションの提供まで、多岐にわたるサービスを提供しています。

0グッド

0クリップ

投稿2021/01/28 09:47

編集2021/01/28 23:35

前提・実現したいこと

実現したいことは、LaravelでGoogleカレンダーの予定を作成する際に**「ゲストを追加」機能**を実装したいです。

LaravelでGoogleカレンダーと連携したシステムを構築しています。laravel-google-calendarというパッケージを使用し、Googleカレンダーを操作する機能を実装しています。Googleの認証にはサービスアカウントを使用しています。PostmanからLaravelにリクエストを投げて機能の検証をしています。

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

  • Googleカレンダーから予定を追加する機能は実装できた

php

1namespace App\Http\Controllers; 2 3use Illuminate\Http\Request; 4use Spatie\GoogleCalendar\Event; 5use Illuminate\Support\Carbon; 6 7class CalendarController extends Controller 8{ 9 public function store() 10 { 11 $event = new Event; 12 $event->name = '検証用'; 13 $event->startDateTime = Carbon::now(); 14 $event->endDateTime = Carbon::now()->addHour(); 15 $event->description = "テスト説明文\nテスト説明文\nテスト説明文"; 16 $event->save(); 17 18 return '成功'; 19 }

予定を追加する機能に、「ゲストを追加」できる機能も実装したいです。そこで、laravel-google-calendaraddAttendeeメソッドを使用するとエラーが表示されます。

php

1 public function store() 2 { 3 $event = new Event; 4 $event->name = '検証用'; 5 $event->startDateTime = Carbon::now(); 6 $event->endDateTime = Carbon::now()->addHour(); 7 $event->addAttendee(['email' => 'test2@gmail.com']); // この一行を追加 8 $event->description = "テスト説明文\nテスト説明文\nテスト説明文"; 9 $event->save(); 10 11 return '成功'; 12 }

返却されたエラー↓

Google\Service\Exception: { "error": { "errors": [ { "domain": "calendar", "reason": "forbiddenForServiceAccounts", "message": "Service accounts cannot invite attendees without Domain-Wide Delegation of Authority." } ], "code": 403, "message": "Service accounts cannot invite attendees without Domain-Wide Delegation of Authority." } } in file /var/www/vendor/google/apiclient/src/Http/REST.php on line 128

実装手順1(Google APIを利用するためのサービスアカウントの設定)

  1. Google Cloud Platformへのログイン
  2. プロジェクトを作成
  3. Google Calendar APIの有効化.
  4. サービスアカウントを作成
  5. サービスアカウントでキーを作成

イメージ説明

参考にした記事

実装手順2(サービスアカウントへのドメイン全体の権限の委任の有効化)

サービスアカウントにドメイン全体の委任を有効

  1. 作成したサービスアカウントに画面遷移
  2. 編集ボタンを押下
  3. 「G Suite ドメイン全体の委任を有効にする」チェックボタンを押下
  4. 保存

サービスアカウントにカレンダースコープを提供

  1. Google Adminにログイン
  2. 「セキュリティ」 → 「APIの制御」 → 「ドメイン全体の委任」に移動
  3. サービスアカウントで発行されたクライアントIDを元にAPIクライアントを追加
  4. 追加したスコープ https://www.googleapis.com/auth/calendar,https://www.googleapis.com/auth/calendar.events,https://www.googleapis.com/auth/admin.directory.resource.calendar

イメージ説明

IAMの設定

  1. Google Cloud PlatformのIAMの画面にログイン
  2. サービスアカウントで作成したメールをIAMにオーナー権限で追加

イメージ説明

実装手順3(Googleカレンダーの設定)

アカウントA(test1@gmail.com)とアカウントB(test2@gmail.com)の設定

  1. カレンダーの追加
  2. カレンダーのアクセス設定
  3. 特定のユーザーとの共有設定でサービスアカウントで発行されたメールをを設定
  4. 権限は「予定の変更」
  5. カレンダーIDの取得

イメージ説明

実装手順4(Laravel)

  • Composerでlaravel-google-calendar "2.2.2"をインストール
  • サービスプロバイダーの設定
  • config/google-calendar.phpの設定

php

1return [ 2 3 /* 4 * Path to the json file containing the credentials. 5 */ 6 'service_account_credentials_json' => storage_path('app/google-calendar/service-account-credentials.json'), 7 8 /* 9 * The id of the Google Calendar that will be used by default. 10 */ 11 'calendar_id' => env('GOOGLE_CALENDAR_ID'), 12];
  • app/google-calendar配下にサービスアカウントでダウンロードしたファイルをservice-account-credentials.json名で保存
  • ルーティングの設定

php

1Route::resource( 2 'calendar', 3 'CalendarController', 4 ['only' => ['index', 'store']] 5);
  • コントローラの設定

php

1namespace App\Http\Controllers; 2 3use Illuminate\Http\Request; 4use Spatie\GoogleCalendar\Event; 5use Illuminate\Support\Carbon; 6 7class CalendarController extends Controller 8{ 9 public function index() 10 { 11 $event = new Event; 12 $events = Event::get(); 13 foreach ($events as $event) { 14 echo $event->name . ''; 15 } 16 } 17 18 public function store() 19 { 20 $event = new Event; 21 $event->name = '検証用'; 22 $event->startDateTime = Carbon::now(); 23 $event->endDateTime = Carbon::now()->addHour(); 24 $event->addAttendee(['email' => 'test2@gmail.com']); 25 $event->description = "テスト説明文\nテスト説明文\nテスト説明文"; 26 $event->save(); 27 28 return '成功'; 29 }
  • .envの設定
GOOGLE_CALENDAR_ID=test1@gmail.com

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

Laravel 5.8.36
PHP 7.3
spatie/laravel-google-calendar 2.2.2

参考にした記事

https://stackoverflow.com/questions/60760959/google-calendar-api-service-account-error
https://issuetracker.google.com/issues/141704931
https://github.com/spatie/laravel-google-calendar/issues/125

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

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

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

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

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

guest

回答1

0

自己解決

「パッケージ」と「認証方法」を変更して、ゲストを追加機能を実装しました。

ゲストを追加するコード(Oauthで認証が通っているのが前提)

php

1use Google_Client; 2use Google_Service_Calendar; 3use Google_Service_Calendar_Event; 4use Google_Service_Calendar_EventAttendee; 5 6private function getClient() 7{ 8 $client = new Google_Client(); 9 $client->setApplicationName('Calendar integration'); 10 $client->setAuthConfig(storage_path('app/google-calendar/hoge.json')); 11 $client->setAccessType("offline"); 12 $client->setIncludeGrantedScopes(true); 13 $client->setApprovalPrompt('force'); 14 $client->setRedirectUri('http://localhost/hoge'); 15 $client->addScope(Google_Service_Calendar::CALENDAR); 16 17 return $client; 18} 19 20$client = $this->getClient(); 21$service = new Google_Service_Calendar($client); 22 23$calendarId = 'primary'; 24$event = new Google_Service_Calendar_Event([ 25 'summary' => '検証用の予定', 26 'start' => [ 27 'dateTime' => Carbon::now() 28 ], 29 'end' => [ 30 'dateTime' => Carbon::now()->addHour(), 31 ] 32]); 33 34$attendeeNew = new Google_Service_Calendar_EventAttendee(); 35$attendeeNew->setEmail('test@gmail.com'); // 招待メールアドレス 36$attendeeNew->setDisplayName("テスト"); 37 38$attendees = $event->getAttendees(); 39array_push($attendees, $attendeeNew); 40$event->setAttendees($attendees); 41 42$event = $service->events->insert($calendarId, $event); 43

投稿2021/02/01 05:14

yuuta_s

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問