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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
sjschminke3
Frequent Visitor

API Connection Won't Authenticate in Power BI Service

I have a report with a single query connected to a web API.  It uses 2 functions, where it grabs a dynamic bearer token first and then passes that token to the API url.  Inside of Power BI this generates 2 web connections, which refresh perfectly fine locally in Desktop.

 

sjschminke3_0-1728498415878.png

 

Here is the query:

=======================================================

let

GetBearerToken = () =>
let
authUrl = "https://login.microsoftonline.com/xxxxxxxxxxxxx/oauth2/v2.0/token",

clientid = "yyyyyyyyyyyyyyyyy",
secret = "zzzzzzzzzzzzzzz",
scope = "wwwwwwwwwwwwww",
granttype = "client_credentials",


requestBody = "grant_type=client_credentials" &
"&client_id=" & Uri.EscapeDataString(clientid) &
"&client_secret=" & Uri.EscapeDataString(secret) &
"&scope=" & Uri.EscapeDataString(scope)
,


authResponse = Json.Document(Web.Contents(authUrl, [Headers = [#"Content-Type"="application/x-www-form-urlencoded"], Content = Text.ToBinary(requestBody)])),

accessToken = authResponse[access_token]

in

accessToken,

GetAPIData = (token as text) =>

let

apiUrl = "https://dlr-xxxxxxxxxxxxxxx.azurewebsites.net/api/Project/xxxxxxxx/Summary",

apiResponse = Json.Document(Web.Contents(apiUrl, [Headers=[Authorization="Bearer " & token]]))

in

apiResponse,

RefreshTokenIfNeeded = (currentToken as text) =>

let

refreshedToken = currentToken

in

refreshedToken

in

GetAPIData(GetBearerToken())

============================================================

When I publish to Power BI service, it tries to authenticate them independently, which does not work because you need to fetch the token to authenticate the API url.  I've tried everything I can think of.  Any ideas?

 

sjschminke3_0-1728499409442.png

 

 

3 ACCEPTED SOLUTIONS
GilbertQ
Super User
Super User

Hi @sjschminke3 

 

You can use the example in the link below from Prathy where she explains how to store the token within a single power query table.

 

Calling Power BI API using Power BI Desktop to document Power BI Service – Prathy's Blog…





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

Proud to be a Super User!







Power BI Blog

View solution in original post

Anonymous
Not applicable

Hi @sjschminke3 ,

 

Thanks for @GilbertQ reply!

 

@sjschminke3 Based on community experience, there are several approaches for you reference.

  • Clear browser cache.
  • Select Skip Test Connection when setting up.

    vmengmlimsft_0-1728893808426.png

 

Here are some links for your reference:

Solved: Credential Error for Excel File hosted on One Driv... - Microsoft Fabric Community

API Unable to refresh in Power BI Service: The cre... - Microsoft Fabric Community

Failed to update data source credentials - Microsoft Fabric Community

 

 

Best regards,

Mengmeng Li

View solution in original post

sjschminke3
Frequent Visitor

In addition to the 2 above responses, I also had to change the data source privacy level to "Organizational" and it finally worked!  Thank you both!

View solution in original post

3 REPLIES 3
sjschminke3
Frequent Visitor

In addition to the 2 above responses, I also had to change the data source privacy level to "Organizational" and it finally worked!  Thank you both!

Anonymous
Not applicable

Hi @sjschminke3 ,

 

Thanks for @GilbertQ reply!

 

@sjschminke3 Based on community experience, there are several approaches for you reference.

  • Clear browser cache.
  • Select Skip Test Connection when setting up.

    vmengmlimsft_0-1728893808426.png

 

Here are some links for your reference:

Solved: Credential Error for Excel File hosted on One Driv... - Microsoft Fabric Community

API Unable to refresh in Power BI Service: The cre... - Microsoft Fabric Community

Failed to update data source credentials - Microsoft Fabric Community

 

 

Best regards,

Mengmeng Li

GilbertQ
Super User
Super User

Hi @sjschminke3 

 

You can use the example in the link below from Prathy where she explains how to store the token within a single power query table.

 

Calling Power BI API using Power BI Desktop to document Power BI Service – Prathy's Blog…





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

Proud to be a Super User!







Power BI Blog

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.