azureoutlookazure-active-directoryazure-functionsmicrosoft-teams

Get Federal User Activity Status Microsoft Graph API


I'm looking for a way to retrieve the activity status of federated users in bulk for external companies via API, without the need to invite the users and wait for them to accept the invitation.

Is it possible to obtain the same activity status response as when using the search bar in Teams, even if not using the Graph API but other methods? Currently, this method only provides an "offline" status or "presence unknown" for all federated users.

Any help or suggestions would be greatly appreciated.

Thank you!

import requests
import json
from dotenv import load_dotenv
from msal import ConfidentialClientApplication


load_dotenv(dotenv_path=r'....')


CLIENT_ID = os.getenv('CLIENT_ID')
CLIENT_SECRET = os.getenv('CLIENT_SECRET')
TENANT_ID = os.getenv('TENANT_ID')

app = ConfidentialClientApplication(
CLIENT_ID,
authority=f"https://login.microsoftonline.com/{TENANT_ID}",
client_credential=CLIENT_SECRET,)

result = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])

if "access_token" in result:
access_token = result["access_token"]
else:
print("Error acquiring token:", result.get("error"), result.get("error_description"))
exit(1)

user_id = "..."

presence_url = f'https://graph.microsoft.com/beta/communications/presences/{user_id}'
presence_headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'}

response = requests.get(presence_url, headers=presence_headers)

if response.status_code == 200:
presence_info = response.json()
print("Presence Info:", json.dumps(presence_info, indent=2))

else:
print("Failed to retrieve presence info:", response.status_code, response.text)

Solution

  • I agree with @Meghana-MSFT, by default it is not possible to fetch presence for External users using Microsoft Graph API. Refer this SO Thread by @alphaz18.

    I got the same response for External user:

    # Initialize the ConfidentialClientApplication
    app = ConfidentialClientApplication(
        CLIENT_ID,
        authority=f"https://login.microsoftonline.com/{TENANT_ID}",
        client_credential=CLIENT_SECRET
    )
    
    # Acquire token for client
    result = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
    
    if "access_token" in result:
        access_token = result["access_token"]
    else:
        print("Error acquiring token:", result.get("error"), result.get("error_description"))
        exit(1)
    
    # User ID for whom presence information is to be retrieved
    user_id = "UserID"  # Replace with the actual user ID
    
    # Presence endpoint URL
    presence_url = f'https://graph.microsoft.com/beta/communications/presences/{user_id}'
    presence_headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json'
    }
    
    # Send request to Microsoft Graph API
    response = requests.get(presence_url, headers=presence_headers)
    
    if response.status_code == 200:
        presence_info = response.json()
        print("Presence Info:", json.dumps(presence_info, indent=2))
    else:
        print("Failed to retrieve presence info:", response.status_code, response.text)
    

    enter image description here

    Reference:

    Guest/external users showing offline - not able to connect - Microsoft Community