Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
beenherebefore
Frequent Visitor

REST API Token Works for GetGruops and GetReports, but fails for GetPages.

Building an Admin report using REST API.

 

I can sucessfully get a new Token and use that to retrieve the Group (Workspace) information and then the Reports for each Workspace. But when I use the list of Report IDs to get pages,  I get an Access Forbidden message.

 

Throwing the call with an embedded report GUID:

https://api.powerbi.com/v1.0/myorg/reports/732a489c-20db-4ac4-8c4a-34c2bf033f81/pages

 

and a newly retrieved Bearer token into Postman, I get the message that the Token is expired:

{
    "error": {
        "code": "TokenExpired",
        "message": "Access token has expired, resubmit with a new access token"
    }
}
 
I don't understand how this can be when I retrieve the Token immediately before embedding into Postman/and Power Bi.
The call DOES work when I use the Token from the Try It section of the REST API documentation.
 
I get the Token in a function which is  called for each table. Is there a limit to the number of times you can get a Token?
 
Flummexed.
 
 
 
Code adapted from BI Elite Youtube channel/Website
 
Function to return Reports (Works fine)
(groupid as text) =>
let
Source = Json.Document(Web.Contents("https://api.powerbi.com/v1.0/myorg/groups/"&groupid&"/reports", [Headers=[Authorization="Bearer "&GetAccessToken()]])),
#"Converted to Table" = Table.FromRecords({Source}),
#"Expanded value" = Table.ExpandListColumn(#"Converted to Table", "value")

in
#"Expanded value"
 
Function to return Page Info (Fails):
(reportid as text) =>
let
Source = Json.Document(Web.Contents("https://api.powerbi.com/v1.0/myorg/reports/"&reportid&"/pages",[Headers=[Authorization="Bearer "&GetAccessToken()]])),
#"Converted to Table" = Table.FromRecords({Source}),
#"Expanded value" = Table.ExpandListColumn(#"Converted to Table", "value")

in
#"Expanded value"
 
Function to return Token

() =>
let
//Found in Portal Azure site under App Registration: PBIAdminAPI-Dev
tenant_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxx", //Overview Tab: Directory/Tenant ID
client_id = "xxxxx-xxxx-xxxx-xxxx-xxxx", //Overview Tab: Application/Client ID
client_secret = "xxxxxxxxxxxxxxxxxxxx", //Certificates & secrets Tab: PowerBI secret Value

//POST Request
url = "https://login.microsoftonline.com/"&tenant_id&"/oauth2/v2.0/token",
body = [
grant_type = "client_credentials",
scope = "https://analysis.windows.net/powerbi/api/.default",
client_id = client_id,
client_secret=client_secret
],

//Get token
GetJson =
Json.Document(
Web.Contents(
url, [
Headers=[
Accept="application/json",
#"Content-Type"="application/x-www-form-urlencoded"
],
Content= Text.ToBinary(Uri.BuildQueryString(body))
]
)
),
access_token = GetJson[access_token]
in
access_token

2 REPLIES 2
GilbertQ
Super User
Super User

Hi @beenherebefore 

 

Instead of trying to write this yourself why not just use this connector built by Miguel who now works at Microsoft?

 

It has got all what you are looking for and more!

 

Power BI REST API Connector — The Power User





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!







Power BI Blog

With respect to my original post, the call https://api.powerbi.com/v1.0/myorg/reports/"&reportid&"/pages  only looks at your personal workspace. If the report  you specify does not exist there, you will get the access error.

 

Your connector works well but interestingly has the same issue as the call.

 

Working with MS Tech support, we found that the API call:

https://api.powerbi.com/v1.0/myorg/groups/"&groupid&"/reports/"&reportid&"/pages

does return the report pages.

 

So you have to call it in a table that contains the Workspace id (Group id) and the Report ID and pass those to the function (which needs to be modified to accept two parameters).

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

Top Solution Authors