Forum Discussion

perhed's avatar
perhed
New Member
5 years ago
Solved

.Net GetActivityEventsAsync fails with Bad Request

Hi!

 

I am trying really hard to download the power bi logs but I'm failing miserabely.

 

The service principal is set to access the Power BI API and the user is a Power BI Admin and has a Power BI Pro license. The service principal also has granted Tenant.Read.All API Permissions. I can get a token and other parts of the API is working. I can for example refresh a dataset.

 

But GetActivityEventsAsync fails with a Bad Request. I have tried many many differents types of ways to call the function but I have not been able to succeed. I have not found any examples on the web of how to call the function properly.

 

Here is an example of the code I am trying. Can anyone please help me??

 

Regards

 

 

//Gets the token for the app registration
//Here we need both a service principal and user identity. The user needs to be admin of the workspace and have Power BI Pro licens.
AuthenticationManager ta = new AuthenticationManager(log);
var tokenCredentials = await ta.GetToken(); //Exception is thrown if

// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri("https://api.powerbi.com"), tokenCredentials))
{
DateTime start = DateTime.Today.AddDays(-1);
DateTime end = start.AddHours(2);

var logs = await client.Admin.GetActivityEventsAsync(string.Format("{0:yyyy-MM-ddTHH:mm:ss.fffZ}", start),string.Format("{0:yyyy-MM-ddTHH:mm:ss.fffZ}", end)); //Fails with bad Request

//Do something with the logs
}

 

Code for Authentication

 

private static readonly string AuthorityUrl = "https://login.windows.net/common/oauth2/token/";
private static readonly string ResourceUrl = "https://analysis.windows.net/powerbi/api";

 

/// <summary>
/// Authenticates the app registration via delegated user.
/// </summary>
/// <returns></returns>
private async Task<OAuthResult> AuthenticateAsync()
{
try
{
Uri oauthEndpoint = new Uri(AuthorityUrl);
using (HttpClient client = new HttpClient())
{
HttpResponseMessage result = await client.PostAsync(oauthEndpoint, new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("resource", ResourceUrl),
new KeyValuePair<string, string>("client_id", ClientId),
new KeyValuePair<string, string>("client_secret", ClientSecret),
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", Username),
new KeyValuePair<string, string>("password", Password),
new KeyValuePair<string, string>("scope", "openid"),
}));

string content = await result.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<OAuthResult>(content);
}
}
catch (HttpRequestException hEx)
{
_log.LogInformation(hEx.Message);
return null;
}
}

  • The problem was solved by enclosing the dates in single quotes:

    var logs = await client.Admin.GetActivityEventsAsync(startDateTime:string.Format("'{0:yyyy-MM-ddTHH:mm:ss.FFFZ}'", loopStart), endDateTime:
    string.Format("'{0:yyyy-MM-ddTHH:mm:ss.FFFZ}'", loopEnd));

    If you use ContinuationToken make sure to both enclose it in sinqle quotes and UrlDecode it before passing it as a parameter.

2 Replies

  • V-lianl-msft's avatar
    V-lianl-msft
    Community Support

    Hi perhed ,

     

    Unfortunately, I couldn't do more testing.

    You could try to create a support ticket. If you have a Pro account it is free.

    https://powerbi.microsoft.com/en-us/support/ 

     

    Best Regards,
    Liang
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

  • The problem was solved by enclosing the dates in single quotes:

    var logs = await client.Admin.GetActivityEventsAsync(startDateTime:string.Format("'{0:yyyy-MM-ddTHH:mm:ss.FFFZ}'", loopStart), endDateTime:
    string.Format("'{0:yyyy-MM-ddTHH:mm:ss.FFFZ}'", loopEnd));

    If you use ContinuationToken make sure to both enclose it in sinqle quotes and UrlDecode it before passing it as a parameter.