Generate Google Calendar Event from Odoo

blog-banner

Introduction

You can create Google Calendar events directly from Odoo without manual syncing. This gives you the option to manage meetings from both Google Calendar and Odoo in real time. When the event is created in Odoo, it is pushed to Google Calendar automatically through the Google Calendar API.

This guide explains how to set up OAuth2 for Google Calendar and includes updated sample code for Odoo 19. The examples work for simple events, recurring events and events with attendees.

Required Python Packages

Install the following modern Google API libraries:

 
pip install google-api-python-client
pip install google-auth
pip install google-auth-oauthlib
pip install google-auth-httplib2
  

These packages replace older libraries like oauth2client, which is deprecated.

Using the Google Calendar API

You can take reference from the following link to generate OAuth2 credentials:
https://developers.google.com/adwords/api/sunset

Use above generated app credentials in ‘GoogleCredentials’ method.

Following are parameters for an event:

  • summary: string, Title of the event
  • start.dateTime, end.dateTime: datetime, The time, as a combined date-time value
  • start.timezone, end.timezone: string, The time zone in which the time is specified
  • attendees: list, The attendees of the event
  • reminders.useDefault: boolean, Whether the default reminders of the calendar refer to the event
  • reminder.overrides: the list, If the event doesn't use the default reminders, this lists the reminders specific to the event, or, if not set, indicates that no reminders are set for this event

To create an event, call the events.insert() method providing at least these parameters:

  • calendarId: Either be the email address or ‘primary’ which will use the primary calendar of the logged in user.
  • event: The event to create with all the necessary details such as start and end.
  • sendNotifications: To ask the Calendar API for sending email notifications to the guests.

Note: If an event is timed then it is better to use start.dateTime and end.dateTime fields. If an event is all-day then it is better to use start.date and end.date fields.

Use the insert() method of the events() service to add the event.

To confirm that the calendar event was created successfully. We can do that by checking the return value, it should be an Event object with all the details we passed.

Now, if you want to check the event was created, one of the fields that's created is a link to the calendar event. You can just use

  • event['htmlLink']

Conclusion

Below is an updated, modern example showing how to authenticate and create a Google Calendar event from Odoo 19.

    import httplib2
    from apiclient import discovery
    from oauth2client import client
    
    credentials = client.GoogleCredentials(access_token,
                cal_client_id,
                cal_client_secret,
                refresh_token,
                update_token_validity,
                "https://accounts.google.com/o/oauth2/token",
                "Google Calendar API Python Quickstart")
    
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)
    event = {}
    event['summary'] =  record.name
    event['start'] =  {
    'dateTime': update_start_date,
    'timeZone': 'Asia/Kolkata',
    }
    event['end'] =  {
    'dateTime': update_end_date,
    'timeZone': 'Asia/Kolkata',
    }
    event['attendees'] =   [
    {'email': 'info@sample.com'},
    ]
    event['reminders'] =  {
    'useDefault': False,
    'overrides': [
     {'method': 'email', 'minutes': 1},
     {'method': 'popup', 'minutes': 10},
     ],
    }
    event = service.events().insert(calendarId='primary', body=event,
    sendNotifications=True).execute()
    print('''*** %r event added:
    Start: %s
    End:   %s''' % (event['summary'].encode('utf-8'), event['start']['dateTime'], 
    event['end']['dateTime']))
    
  • Here is the sample of python code which will help you to generate and send google calendar.
    def send_calendar_invite(self):
    
        ir_attachment = self.env['ir.attachment']
        Mail = self.env['mail.mail']
        
        file_data = "BEGIN:VCALENDAR\n"
        file_data += "PRODID:-//Google Inc//Google Calendar 70.9054//EN\n"
        file_data += "VERSION:2.0\n"
        file_data += "CALSCALE:GREGORIAN\n"
        file_data += "METHOD:REQUEST\n"
        file_data += "BEGIN:VEVENT\n"
        file_data += "SUMMARY:Peg Legge On Leave\n"
        file_data += "DTSTART;TZID=Asia/Kolkata:’2019-01-24’\n"
        file_data += "DTEND;TZID=Asia/Kolkata:’2019-01-24’\n"
        file_data += "TRANSP:TRANSPARENT\n"
        file_data += "END:VEVENT\n"
        file_data += "END:VCALENDAR\n"
      
        attachment_ids = []
        ir_attachment_vals = {}
        mail_values = {}
    
        ir_attachment_vals['type'] = 'binary'
        ir_attachment_vals['mimetype'] = 'text/calendar'
        ir_attachment_vals['name'] = 'invite-calendar'
        ir_attachment_vals['datas_fname'] = 'invite_cal.ics'
        ir_attachment_vals['datas'] = base64.encodestring(file_data)
    
        attachment_id = ir_attachment.create(ir_attachment_vals)
        attachment_ids.append(attachment_id)
    
        mail_values['email_from'] = “fangoso@my-link.cf”
        mail_values['email_to'] = “peg.legge@xumail.ga”
        mail_values['subject'] = “Invitation: Peg Legge On Leave @ Thu Jan 24, 2019 
        9:30am - 7:30pm”
        mail_values['message_type'] = 'email'
        mail_values['auto_delete'] = True
        mail_values['model'] = 'st.google.calendar'
        mail_values['attachment_ids'] = [(6, 0, attachment_ids)]
        today = datetime.datetime.now()
    
        if today.date() <= end_date.date() and record.late_coming != True:
             Mail.create(mail_values)
    
        Mail.process_email_queue()
Contact us

For Your Business Requirements

Contact us