Forum Discussion
System.AuraHandledException: Script-thrown exception while calling the Power BI REST API from apex
- 1 month ago
A 401 Unauthorized from the Power BI REST API almost always indicates that the access token is either invalid for the endpoint or doesn't contain the required permissions.
A few things to verify:
Endpoint
The POST https://api.powerbi.com/v1.0/myorg/GenerateToken endpoint is not the standard endpoint for embedding reports. The endpoint depends on what you're trying to generate a token for.For a report, the typical endpoint is:
POST https://api.powerbi.com/v1.0/myorg/groups/{groupId}/reports/{reportId}/GenerateTokenIf you're using the top-level /GenerateToken endpoint, confirm that it matches your embedding scenario and API version.
Validate the access token
Decode the Microsoft Entra ID access token (for example, using jwt.ms) and verify:The token hasn't expired.
The token contains the required delegated scopes or application roles.
API permissions
Ensure your Microsoft Entra application has the appropriate Power BI API permissions (such as Report.Read.All, Dataset.Read.All, or Report.ReadWrite.All, depending on your scenario), and that admin consent has been granted where required.Embedding prerequisites
Verify that:The report exists.
The workspace ID and report ID are correct.
The authenticated user or service principal has access to the workspace.
If using a service principal, ensure "Allow service principals to use Power BI APIs" is enabled in the Power BI Admin Portal and that the service principal has been granted access to the workspace.
Capture the full response
Even if response.getBody() is null in Apex, log:Response headers
WWW-Authenticate header (if present)
Response status
Correlation/Request IDs (ActivityId or RequestId)
These are extremely useful for diagnosing authentication failures with Microsoft support.
If you can share:
Whether you're using a delegated user or a service principal,
The exact endpoint you're calling,
And how you're acquiring the Entra ID token,
it will be much easier to pinpoint the root cause.
Hi YashMoroliya
We would like to inquire whether have you got the chance to check the solutions provided by Prince0011 in commiunity to resolve the issue. We hope the information provided helps to clear the query. Should you have any further queries, kindly feel free to contact the Microsoft Fabric community.
Thank you.