Reputation: 580
I am trying to create an event using google calendar API (from Service account). Event is creating fine for the calender name '[email protected]'. But When I add attendee, it gives error
"Service accounts cannot invite attendees without Domain-Wide Delegation of Authority."
Configuration
Am I missing anything?
My Code
from __future__ import print_function
import datetime
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly','https://www.googleapis.com/auth/calendar'
,'https://www.googleapis.com/auth/calendar.events.readonly','https://www.googleapis.com/auth/calendar.events']
SERVICE_ACCOUNT_FILE = 'credentials.json'
def main():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
creds =service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
delegated_credentials = creds.with_subject('[email protected]')
service = build('calendar', 'v3', credentials=delegated_credentials)
event = {
'summary': 'Driving Lessons',
'location': 'Driviology Driving school',
'description': 'Usman is Testing',
'start': {
'dateTime': '2022-08-23T09:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2022-08-23T17:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'recurrence': [
'RRULE:FREQ=DAILY;COUNT=2'
],
'attendees': [
{'email': '[email protected]'},
],
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'email', 'minutes': 24 * 60}
],
},
}
event = service.events().insert(calendarId='[email protected]', body=event).execute()
print ('Event created: %s' % (event.get('htmlLink')))
if __name__ == '__main__':
main()
Upvotes: 1
Views: 4310
Reputation: 1057
Domain Wide Delegation on the Admin Console for Google Workspace
You would need to add the Client ID
over your Admin console with the scopes needed to handle the calendar:
https://www.googleapis.com/auth/calendar
https://www.googleapis.com/auth/calendar.events
https://www.googleapis.com/auth/admin.directory.resource.calendar
Upvotes: 1