Home > Mobile >  Downloading Usage Reports From Graph API
Downloading Usage Reports From Graph API

Time:05-19

As part of a C# application I'm working on, I need to include an option to download Teams usage reports for the last 7 days. I've written the following code to try and get the data from Graph:

internal static async Task<string> DownloadUsageReport(AuthenticationResult token)
{
    try
    {
        var client = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
        {
            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
        }));
        var result = await client.Reports.GetTeamsUserActivityUserDetail("D7").Request().GetAsync();
        string reportPath = $"{System.IO.Path.GetTempFileName()}.csv";
        using (var fStream = System.IO.File.OpenWrite(reportPath))
        {
            result.Content.CopyTo(fStream);
        }
        return reportPath;
    }
    catch(Exception ex)
    {
        // some logging
        return string.Empty;
    }
}

The idea is that it should save the output to a CSV file on the computer, and if successful return the path of the file to the calling method. However, when it runs the code fails and I get the following exception message from Graph:

Unexpected character encountered while parsing value: R. Path '', line 0, position 0.

I've searched for the error message, but everything I have found relates to parsing JSON, which I'm not trying to do. Has anyone else experienced this problem getting usage reports from Graph, and if so do you have any advice? Thank you in advance.

CodePudding user response:

It looks like a bug in Microsoft Graph Client Library for .NET, the exception occurs while deserializing response payload.

Could you try this workaround?

var message = client.Reports.GetTeamsUserActivityUserDetail("D7").Request().GetHttpRequestMessage();
var response = await client.HttpProvider.SendAsync(message); 
// response content should be csv
var content = await response.Content.ReadAsStringAsync();
  • Related