我创建了一个 GSuite 帐户,其域名为 redu.club
我使用 GSuite 管理员电子邮件创建了一个项目和一个服务帐户
使用共享设置将该服务帐户添加到管理日历,并授予完全管理权限。
我正在尝试在 redu-admin@redu.club 帐户下创建辅助日历。这是我的代码:
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();
}
}
此代码创建了一个日历,但是当我转到 redu-admin@redu.club 的日历时,我看不到它。我的猜测是它正在服务帐户下创建一个日历。当我尝试添加行时 $this->client->setSubject('redu-admin@redu.club');,得到的错误是:
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."
任何帮助是极大的赞赏。
慕妹3242003