Extracting Microsoft Teams Attendance Data with Graph API
Written on
Chapter 1: Introduction to Microsoft Teams Attendance Tracking
Are you aware that Microsoft Teams keeps track of attendance after a meeting? If you facilitate meetings through this platform, you can monitor who attended, when they joined or left, and even the duration of their participation. This capability extends to both internal team members and external guests.
What exactly is Graph API? Simply put, it’s an API that organizes data as nodes and edges, enabling users to interact with multiple nodes in a single operation. The Microsoft Graph API for Teams offers an endpoint to retrieve Teams-related data for applications.
Before you can access this data, you need to register your application and acquire authentication tokens for either a user or a service. Here’s how to get started.
Section 1.1: Steps to Register Your Application
Step 1: Create an App in Azure Portal
- Navigate to Active Directory and select App registrations.
- Choose the necessary permissions under the application tab.
- Establish a Redirect URI for your application.
- Copy the Client ID, Redirect URI, and Tenant ID into your code or create a .yml file for storage.
Step 2: Authentication
Next, you will need to secure the authorization code, Access Token, and Refresh Token. First, you will create a sign-in through code that allows you to remotely access your work account. This process will generate an authorization code. Use the following code to initiate the authorization code generation:
import json
import requests
import pandas as pd
import yaml
from requests_oauthlib import OAuth2Session
import os
import time
# Load the OAuth settings
with open('oauth_settings.yml', 'r') as stream:
settings = yaml.safe_load(stream)
authorize_url = '{0}{1}'.format(settings['authority'], settings['authorize_endpoint'])
# Function to generate a sign-in URL
def get_sign_in_url():
aad_auth = OAuth2Session(settings['app_id'],
scope=settings['scopes'],
redirect_uri=settings['redirect'])
sign_in_url, state = aad_auth.authorization_url(authorize_url, prompt='login')
return sign_in_url, state
Step 3: Use the Authorization Code
Use the authorization code obtained from the sign-in process to generate Refresh and Access Tokens from the API. Here’s how:
def graphAccessToken():
response = requests.post(url,
headers={'Content-Type': 'application/x-www-form-urlencoded'},
data={
'client_id': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
'scope': 'openid profile offline_access user.read calendars.read Files.Read.All Files.ReadWrite.All Sites.Manage.All Sites.Read.All Sites.ReadWrite.All Directory.Read.All OnlineMeetings.Read ChannelMessage.Read.All Group.Read.All',
'code': 'xxxxxxxxxxxxxxxxxxxxxxxx',
'grant_type': 'authorization_code'
})
response_data = json.loads(response.text)
return response_data
Step 4: Refresh Access Token
Now that you have both the Access and Refresh Tokens, you can refresh the Access Token as follows:
def newAccessToken():
response = requests.post(url,
headers={'Content-Type': 'application/x-www-form-urlencoded'},
data={
'client_id': "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
'scope': 'openid profile offline_access user.read calendars.read Files.Read.All Files.ReadWrite.All Sites.Manage.All Sites.Read.All Sites.ReadWrite.All Directory.Read.All OnlineMeetings.Read ChannelMessage.Read.All Group.Read.All',
'refresh_token': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'grant_type': 'refresh_token'
})
json_response = response.json()
return json_response['access_token']
Chapter 2: Learning Resources
In this episode, we explore how to read Microsoft Teams reports utilizing the Microsoft Graph, offering valuable insights for data extraction.
This video discusses how to call the Graph Reports API using Power Automate, enhancing your skills in automating data retrieval processes.