Reputation: 182
I am using this PHP Google Client Library for creating google calendar events. My requirement is to create a calendar event on the account I created credentials with and also need to create the same event for attendees. This is my code
$credentials = credentials.json';
require '\vendor\autoload.php';
$client = new Google_Client();
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfig($credentials);
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event(array(
'summary' => 'Testing for g calender',
'location' => 'ABC',
'description' => 'A chance to hear more about Google\'s developer products.',
'start' => array(
'dateTime' => '2022-01-05T09:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
),
'end' => array(
'dateTime' => '2022-12-28T17:00:00-07:00',
'timeZone' => 'America/Los_Angeles',
),
'recurrence' => array(
'RRULE:FREQ=DAILY;COUNT=2'
),
'attendees' => array(
array('email' => '[email protected]'),
array('email' => '[email protected]')
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 24 * 60),
array('method' => 'popup', 'minutes' => 10),
),
),
));
$calendarId = 'primary';
$event = $service->events->insert($calendarId, $event);
printf('Event created: %s\n', $event->htmlLink);
The problem is this code only works when i run this without attendees with attendees it is not working and returns me this error
Fatal error: Uncaught Google_Service_Exception: { "error": { "errors": [ { "domain": "calendar", "reason": "forbiddenForServiceAccounts", "message": "Service accounts cannot invite attendees without Domain-Wide Delegation of Authority." } ], "code": 403, "message": "Service accounts cannot invite attendees without Domain-Wide Delegation of Authority." } }
I look for this issue on the internet and find out that i need a G-Suit account in order to enable this Domain Wide Delegation so get the G-Suit account and followed these steps which are mentioned in google documentation.
but still, this does not help me out and I am still unable to activate this Domain Wide Delegation feature on my account. I am still seeing this feature as it is.. ANY HELP !!
Upvotes: 1
Views: 583
Reputation: 116878
Domain wide deligation can only be configured by the google workspace admin. The service account needs to have been created on google cloud console by a user on the domain.
To be 100% clear standard gmail users can't be used for deligation to a service account. Everything must remain within your workspace domain.
Your code also needs to denote the user on your domain the service account should be impersonating
$client->setSubject('[email protected]);
When you configure domain wide delegation to a user on your google workspace account. Your service account will be able to athecate and preform actions on behalf of that user on your google workspace account.
They can then create a new event in the calendar of said user, and it will apear that that user has created the even.
The service account can also invite users to that event. They can be standard Gmail users and users within the domain it does not matter.
A service account can NOT create events in a standard users Gmail account. To create an event in a standard Gmail users account you would need to use Oauth2 and authorize your application to access that users data. service accounts can not be configured to act on behalf of standard gmail users.
Upvotes: 1