Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an event with conference using python and Google Calendar API creates the event but not the conference

I am trying to create an event using Google Calendar API in Python 3. I also want to generate a Google Meet conference link for the event. I am using the documentations provided here:

  • https://developers.google.com/calendar/quickstart/python
  • https://developers.google.com/calendar/v3/reference/events#conferenceData
  • https://developers.google.com/calendar/create-events

The event is created without a problem. However, it is missing the conference link. My code so far is as follows:

from pathlib import Path
from pickle import load
from pickle import dump
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from uuid import uuid4
from typing import Dict, List
from oauth2client import file, client, tools


class EventPlanner:

    def __init__(self, guests: Dict[str, str], schedule: Dict[str, str]):
        guests = [{"email": email} for email in guests.values()]
        service = self._authorize()
        self.event_states = self._plan_event(guests, schedule, service)

    @staticmethod
    def _authorize():
        scopes = ["https://www.googleapis.com/auth/calendar"]
        credentials = None
        token_file = Path("./calendar_creds/token.pickle")

        if token_file.exists():
            with open(token_file, "rb") as token:
                credentials = load(token)

        if not credentials or not credentials.valid:
            if credentials and credentials.expired and credentials.refresh_token:
                credentials.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file('calendar_creds/credentials.json', scopes)
                credentials = flow.run_local_server(port=0)
            with open(token_file, "wb") as token:
                dump(credentials, token)

        calendar_service = build("calendar", "v3", credentials=credentials)

        return calendar_service

    @staticmethod
    def _plan_event(attendees: List[Dict[str, str]], event_time, service: build):
        event = {"summary": "test meeting",
                 "start": {"dateTime": event_time["start"]},
                 "end": {"dateTime": event_time["end"]},
                 "attendees": attendees,
                 "conferenceData": {"createRequest": {"requestId": f"{uuid4().hex}",
                                                      "conferenceSolutionKey": {"type": "hangoutsMeet"}}},
                 "reminders": {"useDefault": True}
                 }
        event = service.events().insert(calendarId="primary", sendNotifications=True, body=event, conferenceDataVersion=1).execute()

        return event


if __name__ == "__main__":
    plan = EventPlanner({"test_guest": "[email protected]"}, {"start": "2020-07-31T16:00:00",
                                                                          "end": "2020-07-31T16:30:00"})
    print(plan.event_states)

I suspect that the problem is with where I have passed conferenceDataVersion but the docs are not exactly clear about where it has to be passed other than that it must be passed. I also tried putting it in the body of the event or in createRequest. It always creates the event but not the conference. Unfortunately, I could not find anything about this online anywhere. Maybe I'm actually that bad at searching, but I have been testing different things and searching for a solution for several days! If anyone knows what I am missing, I will truly appreciate their assistance.

like image 498
Mansour.M Avatar asked Aug 03 '20 19:08

Mansour.M


People also ask

Why I Cannot create an event in Google Calendar?

This usually indicates you do not have permissions to edit or add events to the calendar you picked. To fix this, ask the calendar owner to give you editing privileges on Google Calendar, and then reconnect your Google Calendar account in Zapier.

How do I add an event to API in Google Calendar?

In order to successfully create events, you need to: set your OAuth scope to https://www.googleapis.com/auth/calendar . ensure the authenticated user has write access to the calendar with the calendarId you provided (for example by calling calendarList. get() for the calendarId and checking the accessRole ).

What is the difference between Google event and task?

An Event is placed on your Calendar with an option to invite others and set reminders leading up to the date and time of the occurrence. A Task, on the other hand, is an activity that must be performed by a given date, think of it as an item on your to-do list.


1 Answers

Thanks to @Tanaike, I found what was the problem. The token which is generated the first time the API is authenticated is very specific. The problem I was having turned out to be just with that. As soon as I removed the token and had it get generated again, the problem was solved. That being said, I have no idea why the problem appeared in the first place. I will update the response if I find the reason behind it. But for now, if you are having the same problem, just remove the token and regenerate it.

like image 192
Mansour.M Avatar answered Sep 28 '22 04:09

Mansour.M