Forum Discussion

sjschminke3's avatar
sjschminke3
Frequent Visitor
1 year ago
Solved

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.

 

 

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?

 

 

 

3 Replies