Skip to content

Instantly share code, notes, and snippets.

@lwsrbrts
Last active April 3, 2020 14:38
Show Gist options
  • Save lwsrbrts/dce026c4a203a68f98a8dbe09204f316 to your computer and use it in GitHub Desktop.
Save lwsrbrts/dce026c4a203a68f98a8dbe09204f316 to your computer and use it in GitHub Desktop.
Get your Microsoft Teams presence information from Graph API using an Azure AD App with PowerShell
<#
1. Logged on as a Global Administrator, create a new "App-registration" in Azure AD.
2. Add a Redirect URI. This can point to any web location you like as it isn't used.
2. Add "API permissions" for "Microsoft Graph" to the app for "Presence.Read" and "Presence.Read.All" (Must be delegated).
3. Grant admin consent for the organisation (click the button)
3. Add a new "Secret" to the app which can be used to retrieve a token for the app. Remember to copy the secret!
#>
$clientID = '8806ac69-edde-4069-8128-c01c3aa84eda' # This is the "Application (client) ID" of the application which is granted permission to read presence.
$tenantName = "mytenant.onmicrosoft.com" # This is the name of the tenant for Azure AD.
$ClientSecret = 'qR[QXJ-xB6LjlpZkc[jM]6tB5R4bvebY' # Expires: 03/04/2021. This is the secret (from Certificates & Secrets of the 'Graph' application (app registration)).
$Username = "[email protected]" # The UPN of a user with access to Azure AD - probably you!
$Password = 'StrongPassword1!' # The password of that user.
$ReqTokenBody = @{
Grant_Type = "Password"
client_Id = $clientID
Client_Secret = $clientSecret
Username = $Username
Password = $Password
Scope = "https://graph.microsoft.com/.default"
}
$TokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantName/oauth2/v2.0/token" -Method POST -Body $ReqTokenBody
$apiUrl = 'https://graph.microsoft.com/beta/me/presence'
$Data = Invoke-RestMethod -Headers @{Authorization = "Bearer $($TokenResponse.access_token)"} -Uri $apiUrl -Method Get
$Data.activity
$Data.availability
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment