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

如何编写Python代码结合Azure凭据获取Teams会议参与者列表?

使用Python + Requests获取Teams正在进行的会议参与者列表

前置要求

  • Azure AD应用已添加OnlineMeeting.Read.All应用权限,并完成管理员同意
  • 已获取租户ID(tenantID)、客户端ID(clientID)、客户端密钥(clientSecret)

可运行代码片段

import requests
from datetime import datetime

# 替换为你的Azure AD信息
TENANT_ID = "你的tenantID"
CLIENT_ID = "你的clientID"
CLIENT_SECRET = "你的clientSecret"

def get_access_token():
    """获取Microsoft Graph API的访问令牌"""
    token_url = f"https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token"
    payload = {
        "grant_type": "client_credentials",
        "client_id": CLIENT_ID,
        "client_secret": CLIENT_SECRET,
        "scope": "https://graph.microsoft.com/.default"
    }
    response = requests.post(token_url, data=payload)
    response.raise_for_status()  # 抛出HTTP错误
    return response.json()["access_token"]

def get_active_meetings(token):
    """获取所有正在进行的Teams会议"""
    headers = {"Authorization": f"Bearer {token}"}
    # 用UTC时间过滤:会议已开始且未结束
    now = datetime.utcnow().isoformat() + "Z"
    api_url = f"https://graph.microsoft.com/v1.0/onlineMeetings?$filter=startDateTime le '{now}' and endDateTime ge '{now}'"
    response = requests.get(api_url, headers=headers)
    response.raise_for_status()
    return response.json().get("value", [])

def get_meeting_participants(token, meeting_id):
    """获取指定会议的参与者列表"""
    headers = {"Authorization": f"Bearer {token}"}
    api_url = f"https://graph.microsoft.com/v1.0/onlineMeetings/{meeting_id}/participants"
    response = requests.get(api_url, headers=headers)
    response.raise_for_status()
    return response.json()

if __name__ == "__main__":
    try:
        # 获取令牌
        token = get_access_token()
        print("成功获取访问令牌")

        # 获取正在进行的会议
        active_meetings = get_active_meetings(token)
        if not active_meetings:
            print("当前没有正在进行的Teams会议")
            exit()

        # 遍历会议并获取参与者
        for meeting in active_meetings:
            meeting_title = meeting.get("title", "无标题会议")
            meeting_id = meeting["id"]
            print(f"\n=== 会议:{meeting_title} ===")
            
            participants = get_meeting_participants(token, meeting_id)
            # 解析参与者信息
            attendees = participants.get("attendees", [])
            speakers = participants.get("speakers", [])
            
            print("参会者:")
            for attendee in attendees:
                user_info = attendee["info"]["identity"]["user"]
                print(f"- {user_info['displayName']} ({user_info['userPrincipalName']})")
            
            if speakers:
                print("\n当前发言者:")
                for speaker in speakers:
                    user_info = speaker["info"]["identity"]["user"]
                    print(f"- {user_info['displayName']} ({user_info['userPrincipalName']})")

    except requests.exceptions.HTTPError as e:
        print(f"HTTP请求错误:{e.response.text}")
    except Exception as e:
        print(f"发生错误:{str(e)}")

注意事项

  • 必须确保Azure AD应用的OnlineMeeting.Read.All权限已获得管理员同意,否则会返回权限不足的错误
  • 代码使用UTC时间过滤会议,避免时区差异导致的筛选错误
  • 如果需要获取特定用户的会议,可将get_active_meetings中的API端点改为https://graph.microsoft.com/v1.0/users/{用户objectID}/onlineMeetings?$filter=...,同时需确认应用权限覆盖该场景

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 21:17:25