【发布时间】:2020-08-03 19:54:08
【问题描述】:
我正在尝试使用 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 谢谢你再次帮助我!目标是创建一个包含 google meet 链接的活动。问题是它确实创建了事件,但没有 google meet 链接。在事件正文中包含
createRequest无效。如果我删除它,结果将是相同的。当您说您认为脚本可以创建会议链接时,是猜测还是您运行它时对您有用?另外,你有一个工作的例子吗?如果你这样做,你介意我要求你分享吗?我在网上连一个例子都找不到! -
感谢您的回复。关于你的脚本,当我测试它时,我可以确认会议链接可以包含在创建的事件中。所以我问了你的详细目标。但从你的回复来看,我认为我对你目标的理解是正确的。所以我必须从你的脚本中复制你的情况。但是在当前阶段,我无法复制您的问题。对此我深表歉意。
-
@Tanaike 你是最棒的!因此,当您确认它对您有用时,我意识到问题不在于代码!我开始研究其他的东西,比如我的账户配置等等。事实证明,问题出在 API 生成的令牌文件上。我删除它并让它再次生成,问题就解决了!我希望他们在文档中提到了一些关于它的内容。文档真的是半支持!现在我认为上面的脚本实际上是目前唯一在线可用的示例!
-
感谢您的回复。我很高兴你的问题得到了解决。当您的问题解决后,您可以将其发布为答案吗?这样,它对遇到相同问题的其他用户很有用。
标签: python-3.x google-calendar-api