Unable to receive response from the GetPages Power BI REST API

Ravi Garimella 0 Reputation points
2023-02-10T08:46:47.3833333+00:00

Hi Team, 

 

I am trying to export a PowerBI Embedded Report to PDF using Power BI REST API in ASP.Net Core MVC Project.

 

My Contoller Action Method:

 

[HttpGet]
public async Task<FileContentResult> ExportToPDF()
{
var pageNames = await pbiMgr.GetPageNames();

var exportedFile = await pbiMgr.ExportPowerBIReport(
ConfigValidatorService.ReportId,
ConfigValidatorService.WorkspaceId,
FileFormat.PDF,
5,
CancellationToken.None,
pageNames
);

var memoryStream = new MemoryStream();
exportedFile.FileStream.CopyTo(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);

return File(memoryStream.ToArray(), "application/pdf", "Report.pdf");
}

 

My GetPageNames() code in service class which is called in Controller Action Method:

 

public async Task<IList<string>> GetPageNames()
{
var reportId = ConfigurationManager.AppSettings["reportId"];
var groupId = ConfigurationManager.AppSettings["workspaceId"];
var client = new HttpClient();
var response = await client.GetAsync($"https://api.powerbi.com/v1.0/DataFactZ/reports/{reportId}/pages?groupId={groupId}");
var content = await response.Content.ReadAsStringAsync();

var pageNames = new List<string>();
var pages = JsonConvert.DeserializeObject<Page[]>(content);
foreach (var page in pages)
{
pageNames.Add(page.Name);
}

return pageNames;
}

 

I am not getting the response in the above line await client.GetAsync

I am receiving Error StatusCode: 403, ReasonPhrase: Forbidden. 

 

Can someone help me out with the issue.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,806 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Zhi Lv - MSFT 33,186 Reputation points Microsoft External Staff
    2023-02-13T03:16:37.2933333+00:00

    Hi @Ravi Garimella

    A 403 error can occur for any of the following reasons. A fiddler capture may be required to investigate further.

    • The user has exceeded the amount of embed token that can be generated on a shared capacity. Purchase Azure capacities to generate embed tokens and assign the workspace to that capacity. See Create Power BI Embedded capacity in the Azure portal.
    • The Azure AD auth token expired.
    • The authenticated user isn't a member of the group (workspace).
    • The authenticated user isn't an admin of the group (workspace).
    • The authenticated user doesn't have permissions. Permissions can be updated using refreshUserPermissions API
    • The authorization header may not be listed correctly. Make sure there are no typos.

    The backend of the application may need to refresh the auth token before calling GenerateToken. For more information, see Refresh the access token.

    Since this issue relates Power BI REST API, if still have any further question about it, please ask it on Microsoft Power BI Community forum.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

  2. Ravi Garimella 0 Reputation points
    2023-02-13T10:59:43.1966667+00:00

    Hi Dillion,

    Below is the fiddler response. Can you let me know what is missing.

    HTTP/1.1 403 Forbidden

    Content-Length: 0

    Strict-Transport-Security: max-age=31536000; includeSubDomains

    X-Frame-Options: deny

    X-Content-Type-Options: nosniff

    Access-Control-Expose-Headers: RequestId

    RequestId: ddc28d93-9ef0-4f77-807c-39cb09a1b49a

    Regards

    Ravi


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.