Python msgraph-sdk returns "400 API Error, Unknown Error" occasionally

Shaun Yang (Medalsoft) 0 Reputation points
2024-05-29T05:43:15.25+00:00

I use Python Graph API to search my meeting time by find_meeting_times(). Below is a part of my code, and they are from a class method:

    async def find_meeting_time(
        self,
        start_time: str,
        end_time: str,
        duration: int,
        emails: List[str],
        time_zone: str,
    ):
		credential = ClientSecretCredential(
            os.getenv("AZURE_TENANTID"),
            os.getenv("AZURE_CLIENT_ID"),
            os.getenv("AZURE_CLIENT_SECRET"),
        )
        scopes = ["https://graph.microsoft.com/.default"]
        client = GraphServiceClient(credentials=credential, scopes=scopes)
        body = FindMeetingTimesPostRequestBody(
            attendees=[
                AttendeeBase(
                    type=AttendeeType.Required,
                    email_address=EmailAddress(address=email),
                )
                for email in emails
                if email
            ],
            time_constraint=TimeConstraint(
                activity_domain=ActivityDomain.Work,
                time_slots=[
                    TimeSlot(
                        start=DateTimeTimeZone(
                            date_time=start_time,
                            time_zone=time_zone,
                        ),
                        end=DateTimeTimeZone(
                            date_time=end_time,
                            time_zone=time_zone,
                        ),
                    ),
                ],
            ),
            meeting_duration=timedelta(minutes=duration),
            return_suggestion_reasons=True,
            minimum_attendee_percentage=100.0,
            is_organizer_optional=False,
        )
        config = (
            FindMeetingTimesRequestBuilder.FindMeetingTimesRequestBuilderPostRequestConfiguration()
        )
        config.headers.add("Prefer", f'outlook.timezone="{time_zone}"')
        
        result = await client.users.by_user_id(
                self.my_email
            ).find_meeting_times.post(body=body, request_configuration=config)

It usually worked and gave me the correct result, but it occasionally returns 400 API Error "Unknown Error". Below prints request body that I used to make two requests; one request failed and another succeeded.

FindMeetingTimesPostRequestBody(additional_data={}, attendees=[], is_organizer_optional=False, location_constraint=None, max_candidates=None, meeting_duration=datetime.timedelta(seconds=7200), minimum_attendee_percentage=100.0, return_suggestion_reasons=True, time_constraint=TimeConstraint(additional_data={}, activity_domain=<ActivityDomain.Work: 'work'>, odata_type=None, time_slots=[TimeSlot(additional_data={}, end=DateTimeTimeZone(additional_data={}, date_time='2024-05-30T23:59:59', odata_type=None, time_zone='China Standard Time'), odata_type=None, start=DateTimeTimeZone(additional_data={}, date_time='2024-05-30T00:00:00', odata_type=None, time_zone='China Standard Time'))]))

I am pretty sure that there is no problem with my environmental variables, class attributes, or function parameters. Could someone help me with this issue? Is this an API service issue, authorization issue, or my code issue?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,106 questions
0 comments No comments
{count} votes