Advance your Data & AI career with 50 days of live learning, dataviz contests, hands-on challenges, study groups & certifications and more!
Get registeredGet Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more
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.
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?
Solved! Go to Solution.
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…
Hi @sjschminke3 ,
Thanks for @GilbertQ reply!
@sjschminke3 Based on community experience, there are several approaches for you reference.
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
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!
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!
Hi @sjschminke3 ,
Thanks for @GilbertQ reply!
@sjschminke3 Based on community experience, there are several approaches for you reference.
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
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…
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
Check out the October 2025 Power BI update to learn about new features.