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
rpboyer2
Frequent Visitor

PowerBI REST API & Access Token with Power Query

Greetings,
I'm looking for some input regarding obtaining data from the PowerBI REST API with a function to automate Access Token generation. I can get data from the API with Power Query using the following M code.
PBI Workspace Data with Try It TokenPBI Workspace Data with Try It Token
In this case I'm obtaining the Access Token manually through PowerBI's REST API Documentation 'Try It' feature.
PBI Workspace Try It DocumentationPBI Workspace Try It Documentation
Given i can obtain the expected data with that Power Query M code i keep everything the same but this time leverage the 'GET AccessToken' function i created. I recieve the following error. Selecting 'Connect' does nothing. The error persists.PBI Workspace Data with GET AccessToken FunctionPBI Workspace Data with GET AccessToken Function
Here is my 'GET AccessToken' function code.GET AccessToken FunctionGET AccessToken Function
The function seems to work perfectly fine as it returns the expected Access Token when invoking the function.
GET AccessToken Function InvokedGET AccessToken Function Invoked
I have the following Azure App 'API Permissions' configuredMicrosoft Azure App API permissionsMicrosoft Azure App API permissions
I also am getting Client ID, Tenant ID, Client Secret Value from the Azure App informationMicrosoft Azure App OverviewMicrosoft Azure App OverviewMicrosoft Azure App Certificates & secretsMicrosoft Azure App Certificates & secrets
My 'GET AccessToken' function also works when substituting the values in Postman for testing.PostmanPostman
Everything seems fine independently but when trying to make the connection tying it together it doesnt work. There are enough examples on forums and youtube vidoes which all demonstrate the way i concatenate my invoked function result to the "Bearer " string is correctly done. I'm at a total loss as to what is going wrong. Thank you for your time, any input is greatly appreciated. 

34 REPLIES 34
sergeRLW
Regular Visitor

Hey @rpboyer2,

 

Any chance you can forward that e-mail chain to me too? 

I've been trying to solve this exact same problem for a while now, unsuccessfully unfortunately.

 

I'll pm you my e-mail.

 

Thanks in advance

yagmur1
Frequent Visitor

Hi I have a quick question. 
I followed what you all texted, but I couldn't figure out what the client_secret is. Yes you put client secret in registred application, but what did you saved as a value, where can i find that?

you must create it in the azure portalCapture.PNGCapture2.PNG

Thank you very much for quick respond, but my question was: I don't have any secret created, is that mandatory? Or where can I get that secret. to rephrase, I know how to save a secretes and certificate, but where can i get that secret so that i can save it. I currently don't have a secret saved in my application. Is the secret something like your API key or your some password (and can you say, where can i get those?) 

I cant answer if you absolutely need the secret with certainty. It's been so long since i worked on this that a lot of the details are fuzzy in my memory. I know i needed the secret to get it work for the data i was pulling in my instance. If you want to get a secret please re-examine the picture uploaded. I circled the new client secret option for you.

EduardoHuiza
New Member

Hello there,
For those who want the solution follow the above @rpboyer2 's instructions and just change the POST URL for this:
Old URL: https://login.microsoftonline.com/"<your-tenant>"/oauth2/token
New URL: https://login.microsoftonline.com/"<your-tenant>"/oauth2/v2.0/token

EduardoHuiza_0-1723572821066.png


That work for me. In any case you can contact me on LinkedIn

Have a great day 🙂

GJA
Helper II
Helper II

Hi, 

 

does anyone have the solution to this post ? I have the same issue. I get a token but it doesn't work when I use it in power query. I got the message : DataSource.Error : Web.Contents ... (500) : Internal Server Error.

 

When I use a token generated with try it, it works.

 

Any solution please ?

messes
New Member

Your function is working, but you are missing an item in the body. Using your example I only got returned a empty token missing the roles, when tested with JWT.ms .   After adding the resource field in I started getting a working token and could reuse it (see second script block)

 

(Scope as text, Resource as text) => 
//I moved the other items to parameters which is why you don't see them here. 
let

    //POST Request
    url = "https://login.microsoftonline.com/"&tenant_id&"/oauth2/token",
    body = [
        scope = Scope,
        resource = Resource,
        client_id = client_id,
        client_secret=client_secret,
        grant_type = "client_credentials"
    ],

 

    Source = OData.Feed("https://graph.microsoft.com/beta/security/secureScores?$top=1", 
        [ Authorization = "Bearer " & Text.From(AccessToken("https://graph.microsoft.com/.default","https://graph.microsoft.com"))],
         [ ExcludedFromCacheKey = {"Authorization"}, ODataVersion = 4, Implementation = "2.0" ]),
FionaE
Regular Visitor

Hi

I followed the exact same process but I cannot even get the get access token function to work. My GET Access Token code is exactly the same as yours, but with my own credentials. It just comes up with this message when I click done.

 

FionaE_0-1684729842062.png

Any help would be greatly appreciated.

@rpboyer2 @ams1 

I was able to get past this issue with the help of Microsoft Support. My apologies i havent been able to share the solution in full detail with the community. I've been swamped in both my work and personal life. Contacting Microsoft Support is what i would recommend. Also i can offer to forward you my email chain of discourse with them which might help contextually how i was able to resolve the issue.

Hi, I am facing the same issue. Could you please forward me the emails with MS support to bender42@gmx.de

 

Thanks a lot!

Hi, Please can you send me the email thread with MS Support too - accounts@bettagroup.co.nz

Hi, could you pelase send it to me as well? ismat.zainal@gmail.com

Could you send it to me too please? r2.rebeca@gmail.com

Hello, I have recently been diving into this and I experienced the same issue. Can you please also forward me the the email thread you have with Microsoft Support? Thanks! I'll send you my email address via private message. 

If you could forward this to me as well that would be great. tlepers@positionpartners.com.au

Hi,

 

I'm having the same issues and would appreciate if you could forward the email exchange you had with MS Support. Please send to dospencer@rollins.edu.

Anonymous
Not applicable

hey, I'm running into the same issue, but I'm trying to get Power Automate to generate the token, so that I can run some automations through that way. The access token that I'm generating is significantly shorter than the one generated by the Rest API Try It feature.

Can you email me that same thread from Microsoft Support?

rick.loveday@gmail.com

Thanks so much @rpboyer2 that would be amazing! accounts@bettagroup.co.nz

ams1
Super User
Super User

Hi @rpboyer2 

 

I think the problem is that you used ContentType instead of #"Content-Type"

 

Replace the corresponding part of your code with below:

 

...
                    Headers = [
                        Accept = "application/json",
                        #"Content-Type" = "application/x-www-form-urlencoded"
                    ],
...

 

 

Everything else seems ok - we'll see after you try the above.

 

Please mark this as ANSWER if it helped.

 

P.S.: next time please paste your PowerQuery code as text and not as screenshot 😊

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.