我創建了一個 GSuite 帳戶,其域名為 redu.club我使用 GSuite 管理員電子郵件創建了一個項目和一個服務帳戶使用共享設置將該服務帳戶添加到管理日歷,并授予完全管理權限。我正在嘗試在 [email protected] 帳戶下創建輔助日歷。這是我的代碼:putenv('GOOGLE_APPLICATION_CREDENTIALS=' . __DIR__ . '/redu-service-account.json');define('SCOPES', Google_Service_Calendar::CALENDAR);function createCalendar(){ try { // Create and configure a new client object. $client = new Google_Client(); $client->setApplicationName('Redu'); $client->useApplicationDefaultCredentials(); $client->addScope([SCOPES]); $client->setAccessType('offline'); $service = new Google_Service_Calendar($client); // Calendar creation $calendar = new Google_Service_Calendar_Calendar(); $calendar->setSummary('test'); $calendar->setTimeZone('America/Los_Angeles'); $createdCalendar = $service->calendars->insert($calendar); // Make the newly created calendar public $rule = new Google_Service_Calendar_AclRule(); $scope = new Google_Service_Calendar_AclRuleScope(); $scope->setType("default"); $scope->setValue(""); $rule->setScope($scope); $rule->setRole("reader"); $createdRule = $service->acl->insert($createdCalendar->getId(), $rule); return $createdCalendar->getId(); } catch (Exception $e) { print "An error occurred: " . $e->getMessage(); }}此代碼創建了一個日歷,但是當我轉到 [email protected] 的日歷時,我看不到它。我的猜測是它正在服務帳戶下創建一個日歷。當我嘗試添加行時 $this->client->setSubject('[email protected]');,得到的錯誤是:Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": "unauthorized_client", "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested."任何幫助是極大的贊賞。
1 回答

慕妹3242003
TA貢獻1824條經驗 獲得超6個贊
如果要為用戶創建輔助日歷,則需要模擬該用戶
您已經正確嘗試過:
? ? ? $client = new Google_Client();
? ? ? ? $client->setApplicationName('Redu');
? ? ? ? $client->useApplicationDefaultCredentials();
? ? ? ? $client->addScope([SCOPES]);
? ? ? ? $client->setAccessType('offline');
? ? ? ? $client->setSubject('[email protected]');
? ? ? ? $service = new Google_Service_Calendar($client);
但之前需要遵循兩個重要步驟:
在 GCP 控制臺中為服務帳號啟用域范圍委派
在管理控制臺中為服務帳號提供必要的委派范圍
- 1 回答
- 0 關注
- 114 瀏覽
添加回答
舉報
0/150
提交
取消