Forum Discussion
Graph API: Meeting Attendance Details Organizer and Participant Join/Leave Times
import requests
import msal
# Function to authenticate and obtain an access token
def get_access_token(client_id, client_secret, tenant_id):
authority = f"https://login.microsoftonline.com/{tenant_id}"
app = msal.ConfidentialClientApplication(client_id, authority=authority, client_credential=client_secret)
result = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
if "access_token" in result:
return result["access_token"]
else:
raise Exception("Unable to get access token")
# Function to fetch all users in the organization
def fetch_users(access_token):
url = "https://graph.microsoft.com/v1.0/users"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
users = response.json().get('value', [])
if users:
print("Users found in the organization:")
for user in users:
print(f"User ID: {user['id']}, Display Name: {user['displayName']}")
# Fetch meetings for each user
fetch_meetings(user['id'], access_token)
else:
print("No users found in the organization.")
else:
print(f"Error fetching users: {response.text}")
# Function to fetch the list of online meetings for a user
def fetch_meetings(user_id, access_token):
url = f"https://graph.microsoft.com/v1.0/users/{user_id}/onlineMeetings"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
meetings = response.json().get('value', [])
if meetings:
print(f"Meetings for User ID {user_id}:")
for meeting in meetings:
print(f"Meeting ID: {meeting['id']}, Subject: {meeting['subject']}, Start: {meeting['startDateTime']}")
# Fetch the attendance reports for the meeting
fetch_attendance_reports(user_id, meeting['id'], access_token)
else:
print(f"No meetings found for user ID {user_id}.")
else:
print(f"Error fetching meetings for user ID {user_id}: {response.text}")
# Function to fetch the attendance reports for a specific meeting
def fetch_attendance_reports(user_id, meeting_id, access_token):
url = f"https://graph.microsoft.com/v1.0/users/{user_id}/onlineMeetings/{meeting_id}/attendanceReports"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
reports = response.json().get('value', [])
if reports:
for report in reports:
print(f"Report ID: {report['id']}, Start: {report['meetingStartDateTime']}, End: {report['meetingEndDateTime']}")
# Now you can fetch attendance records using the report_id
fetch_attendance_records(user_id, meeting_id, report['id'], access_token)
else:
print(f"No attendance reports found for meeting ID {meeting_id}.")
else:
print(f"Error fetching attendance reports for meeting ID {meeting_id}: {response.text}")
# Function to fetch attendance records from a report
def fetch_attendance_records(user_id, meeting_id, report_id, access_token):
url = f"https://graph.microsoft.com/v1.0/users/{user_id}/onlineMeetings/{meeting_id}/attendanceReports/{report_id}/attendanceRecords"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
attendance_data = response.json().get('value', [])
print("Attendance Records:")
for record in attendance_data:
print(f"Email: {record['emailAddress']}")
print(f"Role: {record['role']}")
print(f"Total Attendance in Seconds: {record['totalAttendanceInSeconds']}")
for interval in record.get('attendanceIntervals', []):
print(f"Joined: {interval['joinDateTime']}, Left: {interval['leaveDateTime']}, Duration: {interval['durationInSeconds']} seconds")
print("-" * 40)
else:
print(f"Error fetching attendance records for meeting ID {meeting_id} and report ID {report_id}: {response.text}")
# Example usage
if __name__ == "__main__":
# Set up authentication credentials
tenant_id = # Replace with your Tenant ID
client_id = # Replace with your Client ID
client_secret = ' # Replace with your Client Secret
try:
# Get access token
access_token = get_access_token(client_id, client_secret, tenant_id)
# Fetch all users and their meetings and attendance reports
fetch_users(access_token)
except Exception as e:
print(f"Error: {str(e)}")
and this is output formate
User ID: ec6c9e15-9244-4684-a688-9c389223d8bf, Display Name: Trainings ANZ
Error fetching meetings for user ID ec6c9e15-9244-4684-a688-9c389223d8bf: {"error":{"code":"General","message":"No application access policy found for this app.","innerError":{"request-id":"d17b878a-a727-453b-97fd-34d550016759","date":"2025-01-05T13:39:44","client-request-id":"d17b878a-a727-453b-97fd-34d550016759"}}}
User ID: e734e12f-2cd9-4234-8436-f59c66b2454f, Display Name: Veena Gupta
Error fetching meetings for user ID e734e12f-2cd9-4234-8436-f59c66b2454f: {"error":{"code":"General","message":"No application access policy found for this app.","innerError":{"request-id":"74365ee0-5b64-4fa0-b8da-224c4de3b6c5","date":"2025-01-05T13:39:44","client-request-id":"74365ee0-5b64-4fa0-b8da-224c4de3b6c5"}}}
User ID: 38276c27-0aba-4941-a01d-cb33260f8d66, Display Name: Vertika Pant
Error fetching meetings for user ID 38276c27-0aba-4941-a01d-cb33260f8d66: {"error":{"code":"General","message":"No application access policy found for this app.","innerError":{"request-id":"04ea96f8-45a5-4b01-8cfd-d672b6c5881c","date":"2025-01-05T13:39:45","client-request-id":"04ea96f8-45a5-4b01-8cfd-d672b6c5881c"}}}
User ID: 3e02a1f2-7beb-46cd-9dc6-145e1798cc82, Display Name: Vinay Prasad
Error fetching meetings for user ID 3e02a1f2-7beb-46cd-9dc6-145e1798cc82: {"error":{"code":"General","message":"No application access policy found for this app.","innerError":{"request-id":"c8f55c5d-01ab-4776-af22-6689340338cd","date":"2025-01-05T13:39:45","client-request-id":"c8f55c5d-01ab-4776-af22-6689340338cd"}}}
User ID: e51d4bbb-efeb-4129-b895-e59de26687cd, Display Name: Radha V
Error fetching meetings for user ID e51d4bbb-efeb-4129-b895-e59de26687cd: {"error":{"code":"General","message":"No application access policy found for this app.","innerError":{"request-id":"23224170-dcea-4d69-bf92-2b4026ebbafc","date":"2025-01-05T13:39:45","client-request-id":"23224170-dcea-4d69-bf92-2b4026ebbafc"}}}
User ID: 96939574-5a22-49d4-a3c8-3205c92aaa96, Display Name: Wasi Zaman Rizvi
Error fetching meetings for user ID 96939574-5a22-49d4-a3c8-3205c92aaa96: {"error":{"code":"General","message":"No application access policy found for this app.","innerError":{"request-id":"b2137c78-19ca-45df-962f-4903c876d6b1","date":"2025-01-05T13:39:45","client-request-id":"b2137c78-19ca-45df-962f-4903c876d6b1"}}}
PS D:\mentioned>