Forum Discussion

agarrett5's avatar
agarrett5
Copper Contributor
Jul 13, 2023

Retrieve over 28 days of teams call data

Hi,

 

I'm writing a powershell script that is using  graph API to retrieve call data from Teams older than 28 days.  The Error I get is:

 

Invoke-RestMethod : The remote server returned an error: (404) Not Found.
At C:\VSSource\Teams scipt\Teamsdata2.ps1:40 char:13
+ $Response = Invoke-RestMethod -Method Post -Uri $APIUrl -Headers $Hea ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod],
WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodComma
nd

 

The script I have written is (I have removed Tennat ID, Client ID etc)

 

# Set your tenant ID, client ID, and client secret
$TenantId = " "
$ClientId = " "
$ClientSecret = " "

# Constants and endpoint URLs
$Authority = "https://login.microsoftonline.com/$TenantId"
$Resource = "https://graph.microsoft.com"
$Scope = "https://api.interfaces.records.teams.microsoft.com/Teams.VoiceAnalytics" 
$GrantType = "client_credentials"
$AuthTokenUrl = "$Authority/oauth2/token"
$APIUrl = "https://api.interfaces.records.teams.microsoft.com/Teams.VoiceAnalytics/getanalytics"

# Calculate the date 28 days ago
$Date28DaysAgo = (Get-Date).AddDays(-28).ToString("yyyy-MM-dd")

# Generate the access token
$TokenBody = @{
grant_type = $GrantType
client_id = $ClientId
client_secret = $ClientSecret
resource = $Resource
scope = $Scope
}
$AccessToken = (Invoke-RestMethod -Method Post -Uri $AuthTokenUrl -ContentType "application/x-www-form-urlencoded" -Body $TokenBody).access_token

# Set request headers
$Headers = @{
'Authorization' = "Bearer $AccessToken"
'Content-Type' = "application/json"
}

# Create the request body
$RequestBody = @{
startTime = $Date28DaysAgo + "T00:00:00Z"
endTime = (Get-Date).ToString("yyyy-MM-dd") + "T23:59:59Z"
} | ConvertTo-Json

# Make the API call
$Response = Invoke-RestMethod -Method Post -Uri $APIUrl -Headers $Headers -Body $RequestBody -ContentType "application/json"

# Save the data to a CSV file
$Response.values | Export-Csv -Path "CallData.csv" -NoTypeInformation

# Display a message when complete
Write-Host "Call data exported to 'CallData.csv'"

Resources