You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于AWS Lex+Lambda用Python O365库操作Outlook日历的技术问题

O365 Library Guidance & Fix for Your Calendar Availability Error

Hey there! Let's break down your questions and fix that frustrating error you're seeing with the get_availability method.


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) or user.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()
  • 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_availability method 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:
    1. Run pip install O365 --target ./python/lib/python3.x/site-packages (replace 3.x with your Lambda runtime version, e.g., 3.11)
    2. Zip the python folder and upload it as a Lambda Layer
  • Package with Your Code: Zip your Python script along with the installed O365 dependencies. Run pip install O365 --target ./package, copy your script into the package folder, 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.Read or Calendars.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:44:54