这些事件会定期重复。但它们并不是由普通事件产生的。它们是按照时间表精确创建的。
如何使用 Google API 访问它们?
我已经尝试过
service.events().list
和service.events().instances
,但似乎都没有退货。
我正在使用Python 这是我使用 service.events().instances 的代码
import os
from dotenv import load_dotenv
import datetime
import time
import schedule
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
load_dotenv()
SCOPES = ["https://www.googleapis.com/auth/calendar"]
def main():
creds = None
if os.path.exists("token.json"):
creds = Credentials.from_authorized_user_file(
"token.json", SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
"credentials.json", SCOPES
)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open("token.json", "w") as token:
token.write(creds.to_json())
try:
service = build("calendar", "v3", credentials=creds)
now = datetime.datetime.utcnow().isoformat() + 'Z'
week_later = (datetime.datetime.utcnow() +
datetime.timedelta(days=7)).isoformat() + 'Z'
# Call the Calendar API
instances_result = (
service.events()
.instances(
calendarId="primary",
timeMin=now,
timeMax=week_later,
maxResults=100,
eventId='recurringEventId',
)
.execute()
)
instances = instances_result.get("items", [])
if not instances:
print("No upcoming instances found.")
return
# Prints the start and name of the next 10 events
for instance in instances:
start = instance["start"].get(
"dateTime", instance["start"].get("date"))
print(start, instance["summary"])
except HttpError as error:
print(f"An error occurred: {error}")
if __name__ == "__main__":
main()
运行程序后,我得到“未找到即将到来的实例。”
你能找到这个问题的答案吗?我们在从 API 查找活动的预约安排信息时遇到了类似的问题。