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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
kristianjay
Frequent Visitor

Get token from Post API call

Hi guys, 

 

I've tried searching the forum without luck, and I therefore hope that some of you can help me. 

 

I'm trying to pull a token via a Post API call. I can successfully pull the date from the API, but since the token expires I want to do it dynamically. Unfortunately, I'm not even at the dynamic part yet, since my pull request for the token failed. 

 

Once I run the query I get:

kristianjay_1-1664799449684.png

Then I'll try to log in using anonymous and get the following message, thus, it seems that my query has some authentication issues. 

kristianjay_0-1664799820211.png

 

My query is below:

 

let

 

url = "https://login.microsoftonline.com/xxxxxxxxxxxxxxxx/oauth2/v2.0/token",

 

headers = [#"Content-Type" = "application/json"],

 

postData = Json.FromValue([grant_type="client_credentials",

client_secret = "xxxxxxxxxxxxxxxx",
client_id = "xxxxxxxxxxxxxxxx",
scope = "xxxxxxxxxxxxxxxx"]),

 

response = Web.Contents(

url,

 

[

Headers = headers,

Content = postData

]

),

 

access_token=Json.Document(response)

in

 

access_token

1 ACCEPTED SOLUTION
kristianjay
Frequent Visitor

I've found a solution. See below. 

 

let

url = "https://login.windows.net/xxx/oauth2/v2.0/token",

headers = [#"Content-Type" = "application/x-www-form-urlencoded"],

postData = "grant_type=client_credentials&scope=xxx&client_id=xxx&client_secret=xxx",

response = Web.Contents(

url,

[Headers = headers,

Content = Text.ToBinary(postData)

]

),

JsonResponse=Json.Document(response),

access_token = JsonResponse[access_token]

in

access_token

View solution in original post

2 REPLIES 2
kristianjay
Frequent Visitor

I've found a solution. See below. 

 

let

url = "https://login.windows.net/xxx/oauth2/v2.0/token",

headers = [#"Content-Type" = "application/x-www-form-urlencoded"],

postData = "grant_type=client_credentials&scope=xxx&client_id=xxx&client_secret=xxx",

response = Web.Contents(

url,

[Headers = headers,

Content = Text.ToBinary(postData)

]

),

JsonResponse=Json.Document(response),

access_token = JsonResponse[access_token]

in

access_token

Anonymous
Not applicable

Based on the above, I tried something very similar - e.g.:

 

 

    apiUrl = 'https://login.windows.net/<tenant_id>/oauth2/v2.0/token',
    body = [
          client_id='<client_id>',
          grant_type='client_credentials',
          client_secret='<client_secret>',
          scope='api://462...4e7/.default'
    ],
    Source = Json.Document(Web.Contents(apiUrl, [Headers = [Accept = 'application/json'], Content = Text.ToBinary(Uri.BuildQueryString(body))])),
    bearerToken = Source[access_token],

 

 

but it doesn't work in PBI. PBI doesn't give any helpful debug output, so if I run the equivalent from curl, it shows:

 
{'error':'invalid_resource','error_description':'AADSTS500011: The resource principal named api://462...4e7 was not found in the tenant named <tenant_name>. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.
 
Did you do some additional step, e.g. add an "authorized client application" to your app registration (and if so, which one - a PBI app)? add some "app permission" to your app registration (and if so, which one(s))?

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon & SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.