The sample code is in the Python programming language.
First of all, you need to create a project in the Google Cloud Platform Console :
Next, next to the google logo, select the created project:
Since the application will work with google calendar, the project needs to connect the Google Calendar API . Go to the Library :
Find the Google Calendar API and click Enable :
To get unlimited access to google calendar, you need to create a service account. To do this, go to the credentials , Create credentials and select the service account :
Choose a name for the account and click Create :
Then skip the optional settings and return to the tab credentials .
Next, you need to go to the service account information by clicking on the account name and create a key :
It is necessary to select the JSON key file format and a file will be downloaded to the PC, which will be further read by the program to gain access to the service account.
After we go to the calendar, to which you need to grant access to the application, go to its settings:
Next, click Add User , insert the name of the service account and carefully select the permissions (depending on the functionality of the application):
Now let's move on to the application code.
Install the following libraries:
pip install --upgrade google-api-python-client
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2
Then we run the following code:
from __future__ import print_function
import datetime
import googleapiclient
from google.oauth2 import service_account
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/calendar']
calendarId = 'lp285psodk309lilp73d9irek8@group.calendar.google.com'
SERVICE_ACCOUNT_FILE = 'light-reality-285313-acdf4768fc46.json'
class GoogleCalendar(object):
def __init__(self):
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
self.service = googleapiclient.discovery.build('calendar', 'v3', credentials=credentials)
#
def create_event_dict(self):
event = {
'summary': 'test event',
'description': 'some info',
'start': {
'dateTime': '2020-08-03T03:00:00+03:00',
},
'end': {
'dateTime': '2020-08-03T05:30:00+03:00',
}
}
return event
#
def create_event(self, event):
e = self.service.events().insert(calendarId=calendarId,
body=event).execute()
print('Event created: %s' % (e.get('id')))
#
def get_events_list(self):
now = datetime.datetime.utcnow().isoformat() + 'Z'
print('Getting the upcoming 10 events')
events_result = self.service.events().list(calendarId=calendarId,
timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])
calendar = GoogleCalendar()
print("+ - create event\n? - print event list\n")
c = input()
if c == '+':
event = calendar.create_event_dict()
calendar.create_event(event)
elif c == '?':
calendar.get_events_list()
else:
pass
As a result of executing the above code with the '+' parameter, we see the created event in the calendar:
Links to materials: