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
Founder
Frequent Visitor

Creating an embedded report with a fabric datasource using service principle authentication

I have an app that displays power bi reports from a workspace.

When I use fabric as a datasource, I get an error in 

 

 

var tokenResponse =
                await client.Reports.GenerateTokenAsync(
                    new Guid(SelectedWorkspaceId),
                    new Guid(SelectedReportId),
                    generateTokenRequestParameters);

 

 

 

Founder_0-1712214254149.png

 

I can get an fabric access token using a service principle, but this doesn't work when passed to Show report

 

 

 

  string[] scopes = new string[] { "https://api.fabric.microsoft.com/.default" };

  ConfidentialClientApplicationBuilder confidentialClientAppBuilder =
      ConfidentialClientApplicationBuilder.Create(ClientId)
      .WithClientSecret(ClientSecret)
      .WithAuthority(Authority)
      .WithTenantId(tenantId);

  IConfidentialClientApplication confidentialClientApplication = confidentialClientAppBuilder.Build();

  AuthenticationResult result = await confidentialClientApplication.AcquireTokenForClient(scopes)
      .ExecuteAsync()
      .ConfigureAwait(false);

  
  return result.AccessToken;

 

 

 

3 REPLIES 3
v-shex-msft
Community Support
Community Support

HI @Founder,

It seems like you faced the 400 error, this error occurs when a request sent to a website server is incorrect or corrupt, and the server receiving the request can’t understand it.

rest - 400 BAD request HTTP error code meaning? - Stack Overflow

AFAIK, the root URL of rest API should be https://api.powerbi.com/v1.0/myorg. According to your descriptions, it seems like you modify it to https://api.fabric.microsoft.com/ and it did not able to be responded with these requests.

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

 

 using (var client = new PowerBIClient(
     new Uri("https://api.powerbi.com/"), tokenCredentials))
 {
     var report =
     await client.Reports.GetReportInGroupAsync(
         new Guid(SelectedWorkspaceId),
         new Guid(SelectedReportId));

     var generateTokenRequestParameters =
         new GenerateTokenRequest(accessLevel: TokenAccessLevel.View);

     var tokenResponse =
         await client.Reports.GenerateTokenAsync(
             new Guid(SelectedWorkspaceId),
             new Guid(SelectedReportId),
             generateTokenRequestParameters);

     await Interop.ShowReport(
         JSRuntime,
         PowerBIElement,
         tokenResponse.Token,
         report.EmbedUrl,
         report.Id.ToString());
 }

 

 

This is the code I'm using to get the token. This is what is producing the error

HI @Founder,

So you mean these processes get the return the error messages that you faced and motion at the above? Have you tried to create a common function to invoke these operations instead of package into the using functions?

The GC may affect the await processings, perhaps you can add some output trace to confirm these required parameters has correctly generated in your codes processings.

BTW, are you worked with fabric API? AFAIK, current it seems not support service principal.

Microsoft Fabric REST APIs for automation and embedded analytics - Microsoft Fabric REST APIs | Micr...

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help 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.