Search code examples
phpgoogle-calendar-apigoogle-api-php-clientgoogle-directory-api

Super Admin service account not finding resource calendar


I'm attempting to list all events from a Google Workspace Resource Calendar which results in either a "user not found" or "invalid impersonation" depending on which calendar identifier I use. The same code works for normal users but not specifically resource calendars.

$client = new Client();
$client->setApplicationName($this->getName());
$client->setAuthConfig("{$this->configLocation}/{$this->keyReceiving}"); // Service Account JSON Key
$client->setSubject($resource->getResourceEmail()); // Conference room email address
$client->setScopes($this->scopes); // [Calendar::CALENDAR, Calendar::CALENDAR_EVENTS, Directory::ADMIN_DIRECTORY_RESOURCE_CALENDAR]

$calendarService = new Calendar($client);
$events = $calendarService->events->listEvents("primary");

The difference between "normal users" and resources is that the domain for resources is @resource.calendar.google.com and users is @domain.com where the domain is my own. This results in a "User not found" error. Edit: All resources are owned by the same organization as my service account key and domain wide delegation is generated for.

If I instead change the subject to $resource->getResourceID() which is a numeric ID instead of an email address it results in a "Invalid impersonation" error.

If I use my own Super Admin account I can list any normal user calendar, but if I attempt to list a resource calendar either by ID or Email I get the error "404 Not Found" - as if the calendar object does not exist.

Is the above approach correct for listing (and later updating) a resource calendar events?


Solution

  • This error appears because the setSubject() method is using a Resource, this method is only used to impersonate other users in order to perform the API call on their behalf.

    When using a resource calendar the code is telling Google "I'm this conference room please get my primary calendar". You can try this instead:

    $client->setSubject($adminEmailAddress); // A Super admin email address
    
    ...
    
    $events = $calendarService->events->listEvents($resource->getResourceEmail());