Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago

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);

 

 

 

 

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

  • Anonymous's avatar
    Anonymous
    Not applicable

    HI Anonymous,

    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

    • Anonymous's avatar
      Anonymous
      Not applicable

       

       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

      • Anonymous's avatar
        Anonymous
        Not applicable

        HI Anonymous,

        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 | Microsoft Learn

        Regards,
        Xiaoxin Sheng