为了账号安全,请及时绑定邮箱和手机立即绑定

在 GSuite 域中创建辅助日历

在 GSuite 域中创建辅助日历

PHP
蓝山帝景 2023-06-24 17:45:29
我创建了一个 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."任何帮助是极大的赞赏。
查看完整描述

1 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

如果要为用户创建辅助日历,则需要模拟该用户

您已经正确尝试过:

      $client = new Google_Client();

        $client->setApplicationName('Redu');

        $client->useApplicationDefaultCredentials();

        $client->addScope([SCOPES]);

        $client->setAccessType('offline');

        $client->setSubject('redu-admin@redu.club');

        $service = new Google_Service_Calendar($client);

但之前需要遵循两个重要步骤:

  1. 在 GCP 控制台中为服务帐号启用域范围委派

  2. 在管理控制台中为服务帐号提供必要的委派范围


查看完整回答
反对 回复 2023-06-24
  • 1 回答
  • 0 关注
  • 65 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信