LoginSignup
1
0

More than 3 years have passed since last update.

Google Calendar APIでイベント登録時に参加者を設定した時だけエラーになる

Posted at

環境

  • PHP:7.4.7
  • google/apiclient:2.7.0

前提

  • サービスアカウントのキーファイル取得済み
  • G Suiteドメイン全体の委任を有効化済
  • https://www.googleapis.com/auth/calendarスコープのアクセス許可済み
  • イベント参加者は自ドメイン内の人

症状

参加者のいるイベント登録しようとすると以下のエラーメッセージが表示される。参加者がいないと問題なく登録される。

Service accounts cannot invite attendees without Domain-Wide Delegation of Authority.

原因

サービスアカウントそのままでは登録できない仕様らしい。
impersonate(日本語だとユーザーアカウントの成り代わり?)が必要だった。

サンプルコード

sample.php
/**
* @param string $calendarID イベント登録するカレンダーのID
* @param Google_Service_Calendar_Event $event イベント情報
*/
public function insertEvent($calendarID, $event) {
  $client = new Google_Client();
  $client->setApplicationName('Sample Calendar');
  $client->setScopes(array(Google_Service_Calendar::CALENDAR));
  $client->setAuthConfig('キーファイルのパス'));
  $client->setSubject('hoge@example.com');     // ←これが必要だった。
  $service = new Google_Service_Calendar($client);
  $service->events->insert($calendarId, $event);
}

参考

1
0
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
1
0