Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
perhed
New Member

.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;
}
}

1 ACCEPTED SOLUTION
perhed
New Member

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.

View solution in original post

2 REPLIES 2
perhed
New Member

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.

V-lianl-msft
Community Support
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/ 

Support Ticket.gif

 

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

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.