使用python和Google Calendar API创建一个带有会议的事件,创建事件但不创建会议



我正在尝试在Python 3中使用Google Calendar API创建一个事件。我还想为该活动生成一个Google Meet会议链接。我使用这里提供的文件:

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

创建事件时没有出现任何问题。但是,它缺少会议链接。到目前为止,我的代码如下:

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": "test.guest@gmail.com"}, {"start": "2020-07-31T16:00:00",
    "end": "2020-07-31T16:30:00"})
print(plan.event_states)

我怀疑问题出在我传递conferenceDataVersion的地方,但除了必须传递之外,文档并不完全清楚它必须传递到哪里。我还试着把它放在事件的正文或createRequest中。它总是创建事件,而不是会议。不幸的是,我在任何地方都找不到关于这件事的任何信息。也许我真的不擅长搜索,但几天来我一直在测试不同的东西并寻找解决方案!如果有人知道我错过了什么,我将非常感谢他们的帮助。

感谢@Tanaike,我发现了问题所在。第一次验证API时生成的令牌非常具体。事实证明,我遇到的问题就是这个。一旦我删除了令牌并再次生成它,问题就解决了。话虽如此,我一开始不知道为什么会出现这个问题。如果我找到原因,我会更新响应。但现在,如果你有同样的问题,只需删除令牌并重新生成它。

  1. 有了这些代码,我们可以为我创建会议(谷歌会议链接(,它可以工作
"conferenceData": {
"createRequest": {
"requestId": "SecureRandom.uuid"
}
}

最新更新