Man*_*r.M 7 google-calendar-api python-3.x
我正在尝试使用 Python 3 中的 Google Calendar API 创建活动。我还想为该活动生成 Google Meet 会议链接。我正在使用此处提供的文档:
该事件创建没有问题。但是,它缺少会议链接。到目前为止我的代码如下:
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)
Run Code Online (Sandbox Code Playgroud)
我怀疑问题出在我已经通过的地方conferenceDataVersion,但文档除了必须通过之外并不清楚它必须通过的地方。我还尝试将其放入事件正文或createRequest. 它总是创建事件而不是会议。不幸的是,我在网上找不到任何关于此的信息。也许我实际上不擅长搜索,但几天来我一直在测试不同的东西并寻找解决方案!如果有人知道我缺少什么,我将非常感谢他们的帮助。
感谢@Tanaike,我找到了问题所在。第一次对 API 进行身份验证时生成的令牌非常具体。事实证明我遇到的问题就是这个。当我删除令牌并再次生成它时,问题就解决了。话虽这么说,我不知道为什么会出现这个问题。如果我找到背后的原因,我会更新回复。但目前,如果您遇到同样的问题,只需删除令牌并重新生成即可。
| 归档时间: |
|
| 查看次数: |
4156 次 |
| 最近记录: |