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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
kasiaw29
Resolver II
Resolver II

Connecting to REST API - Trouble authenticating

Hi community,

 

I'm trying to connect to some REST API's to get data on my PowerBI Workspaces activity in order to get Usage Metrics but per whole real estate. 

I'm using a function that gets a refreshable access token, when it's invoked and refreshed it returns a new token. 

kasiaw29_0-1623397438263.png

 

Then I'm passing it into my Queries for workspaces, datasets and reports

kasiaw29_2-1623397540363.png

 

but I'm getting this when it's asking me to specify how to connect:

kasiaw29_1-1623397490144.png

 

So somewhere it is failing on passing the access token through. Has anyone tried to accomplish what I'm trying before? Did you run into the same issue? Can you help a girl out, please? 

 

Thanks,

Kasia 

4 REPLIES 4
allan_oliveira
Frequent Visitor

Hi....
i need to build a get access token function, could you share yours function?
thank you so much

Hey @allan_oliveira 

 

Sure so what I did is created 3 parameters where I stored my App ID, Tenant ID and App Secret 

kasiaw29_0-1623653314731.png

You can do that by going to Manage Parameters at the top and create a new to store those details. 

Then I have created Get Access Token function, using this code: 

() =>

let
    body = "client_id=" & #"App ID" 
    & "&scope=https://analysis.windows.net/powerbi/api/.default&client_secret=" & #"App Secret" 
    & "&grant_type=client_credentials",
    Data= Json.Document(Web.Contents("https://login.microsoftonline.com/"& TenantID & "/oauth2/v2.0/token/", 
    [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(body)])),
    access_token = Data[access_token]
in
    access_token

As you can see I'm referencing my parameters in the code. 

To test that this works for you press Invoke and you should get a long string with token passing through. You can refresh it and you'll see each time it returns a fresh token. 

 

This is as far as I have it working, my issue is passing it into workspace code...it's having trouble authenticating:

 

kasiaw29_1-1623653580231.png

 

 

Hi @kasiaw29 

I have a test on your access token code, it works well.

1.png

I think you meet problem when you use this token to get result in your workspace code. You can try to use Web connector to connect to your Group API and then change the access token to dynamic in Advance Editor.

1.png

2.png3.png

For more details, you may refer to this video and you can try the way in this video to get access token too.

Video: Call Power BI Rest API with Dynamic Access Token From Power BI Desktop 

 

Best Regards,

Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 

 

Hi just getting back to this. It's exactly what I have in my query for workspaces, still doesn't work. 

kasiaw29_0-1631635322761.png

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.