Call the ListCalendars API operation to retrieve a calendar ID from the return value. Pass the calendar ID to the EventView API operation and set the start and end dates to retrieve an event ID. Then, pass the event ID to the GetEvent API operation to retrieve the final event details.
Related API operations
Basic flow

Python code example
API Open Platform: Code sample to obtain an access credential
Important
Important: This code was tested in Python 3.11.9. Test the code thoroughly before using it in a production environment.
# -*- coding: utf-8 -*-
import requests
# Import the function to get the access token. Modify the path as needed, or directly assign a value to access_token for testing.
from api_demo.get_access_token import access_token
def get_calendar_folders(v_name):
"""
Get the list of calendar folders.
https://mailhelp.aliyun.com/openapi/index.html#/operations/alimailpb_alimail_mailagent_open_CalendarService_ListCalendars
"""
url = f"https://alimail-cn.aliyuncs.com/v2/users/{v_name}/calendars"
querystring = {}
headers = {'Content-Type': 'application/json', 'Authorization': 'bearer ' + access_token}
response = requests.request("GET", url, headers=headers)
print('##########################################################################')
print('Request parameters:', querystring)
print('Return values:', response.status_code, response.text)
print('##########################################################################')
return response.json()["calendars"]
def get_calendar_view(v_name, v_calendar_id, querystring):
"""
Query the calendar view.
https://mailhelp.aliyun.com/openapi/index.html#/operations/alimailpb_alimail_mailagent_open_EventService_EventView
"""
url = f"https://alimail-cn.aliyuncs.com/v2/users/{v_name}/calendars/{v_calendar_id}/eventsview"
headers = {'Content-Type': 'application/json', 'Authorization': 'bearer ' + access_token}
response = requests.request("GET", url, headers=headers, params=querystring)
print('##########################################################################')
print('Request parameters:', querystring)
print('Return values:', response.status_code, response.text)
print('##########################################################################')
return response.json()["events"]
def get_calendar_detail(v_name, v_calendar_id, v_event_id):
"""
Get the event.
https://mailhelp.aliyun.com/openapi/index.html#/operations/alimailpb_alimail_mailagent_open_EventService_GetEvent
"""
url = f"https://alimail-cn.aliyuncs.com/v2/users/{v_name}/calendars/{v_calendar_id}/events/{v_event_id}"
querystring = {}
headers = {'Content-Type': 'application/json', 'Authorization': 'bearer ' + access_token}
response = requests.request("GET", url, headers=headers)
print('##########################################################################')
print('Request parameters:', querystring)
print('Return values:', response.status_code, response.text)
print('##########################################################################')
# Get the calendar list.
email_account = 'test@example.com'
list_calendars = get_calendar_folders(email_account)
print(list_calendars)
for calendar in list_calendars:
# Get the calendar ID and name.
print(calendar["id"], calendar["name"])
print('Finished getting calendar IDs.')
# Select a calendar as needed.
calendar_id = list_calendars[0]["id"] # Modify as needed.
time_range = {"startTime": "2024-08-01T14:15:22Z", "endTime": "2024-12-31T14:15:22Z"}
list_events = get_calendar_view(email_account, calendar_id, time_range)
print(list_events)
if len(list_events) > 0:
for events in list_events:
# Get the event_id, subject, and other details.
print(events["id"], events["subject"])
print('Finished querying calendar view for event_id.')
# Get the event details.
event_id = list_events[0]["id"] # Modify as needed.
get_calendar_detail(email_account, calendar_id, event_id)
print('Finished getting event details.')
else:
print('No data found. Check parameters such as the time range.')
Result

该文章对您有帮助吗?