基于AWS Lex+Lambda用Python O365库操作Outlook日历的技术问题
Hey there! Let's break down your questions and fix that frustrating error you're seeing with the get_availability method.
1. O365 Official Docs & Calendar-Related Functions
First off, the O365 library does have comprehensive official documentation covering all calendar features—you don't need to handcraft raw API requests (though the library wraps Microsoft Graph API under the hood, using standard GET/POST/PUT/DELETE requests as needed).
Key Calendar Functions to Know:
- Access a user's schedule: Start with either
account.schedule()(for delegated user auth) oruser.schedule()(for service principal/client credentials auth, which is what you're using). - Manage calendars:
- Fetch the default calendar:
schedule.get_default_calendar() - List all calendars for the user:
schedule.list_calendars()
- Fetch the default calendar:
- Create appointments:
calendar = schedule.get_default_calendar() new_event = calendar.new_event() new_event.title = 'Client Check-In' new_event.start = dt.datetime(2024, 5, 20, 10, 0, tzinfo=dt.timezone.utc) new_event.end = dt.datetime(2024, 5, 20, 11, 0, tzinfo=dt.timezone.utc) new_event.attendees.add('attendee@example.com') new_event.save() - Check availability: The
get_availabilitymethod requires the right user context (more on this in the error fix section).
2. Using O365 in AWS Lambda
AWS Lambda does not come with the O365 library pre-installed. You have two straightforward ways to deploy it:
- Lambda Layers: Create a layer containing the O365 package and attach it to your Lambda function. To build the layer:
- Run
pip install O365 --target ./python/lib/python3.x/site-packages(replace3.xwith your Lambda runtime version, e.g., 3.11) - Zip the
pythonfolder and upload it as a Lambda Layer
- Run
- Package with Your Code: Zip your Python script along with the installed O365 dependencies. Run
pip install O365 --target ./package, copy your script into thepackagefolder, zip everything, and upload to Lambda.
3. Fixing the get_availability 400 Error
The error Resource not found for the segment 'calendar' happens because you're using client credentials auth (service principal) but calling account.schedule().get_availability(). With this auth type, you can't access a generic "calendar" endpoint—you need to target a specific user's calendar explicitly.
Corrected Code:
from O365 import Account, User import datetime as dt # Replace with your actual credentials credentials = ('xxxxx-0d57-49e5-xxxx-xxxxxxxx', 'xxxxx^[xxxxxxx[%') tenant_id = 'xxxxxx-e1ec-xxxxxxx-b1d9-xxxxxxxxx' target_user_email = 'test@angkorwat.onmicrosoft.com' account = Account(credentials, auth_flow_type='credentials', tenant_id=tenant_id) if account.authenticate(): print('Authenticated!') # Fetch the specific user object first user = User(target_user_email, account=account) # Get the user's schedule (not the account's) schedule = user.schedule() # Important: Pass timezone-aware datetime objects start_time = dt.datetime(2019, 9, 5, 19, 45, tzinfo=dt.timezone.utc) end_time = dt.datetime(2019, 9, 10, 0, 0, tzinfo=dt.timezone.utc) # Now call get_availability correctly availability = schedule.get_availability([target_user_email], start_time, end_time) print(availability)
Additional Checks:
- Make sure your Azure AD app registration has the application permissions
Calendars.ReadorCalendars.ReadWrite(delegated permissions won't work with client credentials auth). - Confirm your Azure AD admin has granted consent for these permissions.
- Always use timezone-aware datetime objects to avoid Graph API errors related to time zone mismatches.
内容的提问来源于stack exchange,提问作者Vanjith

