Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago

Getting error when calling Power BI Embed Report Export To PDF API

We are using Power BI c# SDK to export Power BI report in PDF.
We setup app registration in Azure Active Directory and assigned the required permissions. We Have power-bi-embedded A4 Plan
Some time pdf is generated and some time its not.
When its not generated, following error is thrown:
"Operation returned an invalid status code 'Forbidden'".


Code

var tenantSpecificURL = this.PowerBIConfig.AuthorityUrl.Replace("common", this.PowerBIConfig.TenantId, StringComparison.InvariantCultureIgnoreCase);
var authenticationContext = new AuthenticationContext(tenantSpecificURL);
var credential = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(this.PowerBIConfig.ApplicationId, this.PowerBIConfig.ApplicationSecret);
var authenticationResult = await authenticationContext.AcquireTokenAsync(this.PowerBIConfig.ResourceUrl, credential).ConfigureAwait(false);

var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
PowerBIClient pbiClient = new PowerBIClient(new Uri(this.PowerBIConfig.ApiUrl), tokenCredentials);
Export export = pbiClient.Reports.ExportToFileInGroup(new Guid(this.PowerBIConfig.WorkspaceId), new Guid(reportID), exportRequest);
string exportId = export.Id;
do
{
export = pbiClient.Reports.GetExportToFileStatusInGroup(new Guid(this.PowerBIConfig.WorkspaceId), new Guid(reportID), exportId);
Console.WriteLine(" - Export status: " + export.PercentComplete.ToString() + "% complete");
}
while (export.Status != ExportState.Succeeded && export.Status != ExportState.Failed);

if (export.Status == ExportState.Failed)
{
Console.WriteLine("Export failed!");
}

4 Replies

  • AmosHersch's avatar
    AmosHersch
    Microsoft Employee

    Hi Anonymous ,

    Do you mean that the call to the "Export to File" API returns Forbidden?

    Did you check to see if the response body has additional information for the reason of the Forbidden status?

    If the response body is empty or doesn't help with understanding the issue you can reply here with the request ID from the response headers and I might be able to find the reason.

  • Anonymous's avatar
    Anonymous
    Not applicable


    Operation returned an invalid status code 'Forbidden'||   at *** at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Logged|12_1(ControllerActionInvoker invoker)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\r\n   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n   at *** in ***

    • AmosHersch's avatar
      AmosHersch
      Microsoft Employee

      This looks like the exception when the error is returned, what I meant is the Http response message itself, it has headers and body. Maybe you can capture using Fiddler or other tool. Without the response message I can't help much