Forum Discussion
API Graph Python: Get Calendar Events - ODataError
Hello,
for an application I would like to extract the calendar events for the upcoming 10 days of specific calendars within my company.
Therefore I followed the Tutorial "Build Python app with Microsoft Graph and app-only authentication.
https://learn.microsoft.com/de-de/graph/tutorials/python-app-only?tabs=aad
The example code is working - I get an access token and also the users are listed.
However - when I integrate my own API call I get the error:
msgraph.generated.models.o_data_errors.o_data_error.ODataError
Our Admin provided the Application Permissions: Calendars.Read, Calendars.ReadBasic.All
To extract the Events from a specific calender I use an adapted version of the last example of the following page: https://learn.microsoft.com/en-us/graph/sdks/create-requests?tabs=Python
The code (which causes the error in line 12) in the graph.py is the following:
from msgraph.generated.users.item.calendar.calendar_view.calendar_view_request_builder import CalendarViewRequestBuilder
import datetime as dt
async def get_calendar_events(self):
start_date = dt.date.today()
end_date = dt.date.today() + dt.timedelta(days=1)
query_params = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetQueryParameters(
start_date_time=str(start_date),
end_date_time=str(end_date),
)
request_config = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetRequestConfiguration(
query_parameters=query_params,
)
calendar = await self.app_client.users_by_id('email address removed for privacy reasons').calendar_view.get(request_configuration=request_config)
print(calendar)
return calendar
The code in the main.py is the following:
async def list_calendar_events(graph: Graph):
calendar = await graph.get_calendar_events()
print(calendar)
for event in calendar.value:
print("Event ID: ", event.id)
print("Start: ", event.start)
return
What is the error and how can I correct it?
Thank you!