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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
shahnavazkazi
Employee
Employee

Get data from AAD protected REST web API

We are developing an app for which there is a need to show power BI tiles/reports in it. The app can connect to Power BI by getting authentication through AAD as show in the diagram (step 3 in below image).

 

My question is regarding step 4, where in Power BI needs to talk to REST web APIs which pass data in JSON. These web APIs are AAD protected and the reports are all user specific so how can power BI pass the authorization bearer token to the web APIs which it recieves from the app or is it possible for Power BI to get authenticated through AAD first and then passes over the token to web APIs so that web APIs can get user specific data based on the token and return to Power BI?

 

 

PowerBI_Integration.png

4 REPLIES 4
ss78
New Member

I had the same issue. Here's the workaround I came up with (only tested in Power BI desktop):

 

1. Using Query Editor in PowerBI desktop, create a new query using R language as the data source (this data source is found in Other).

    1. The query using R as the data source basically invokes a PowerShell script that uses the ADAL library to authenticate with AAD and get a bearer token, which is usually valid for a small period of time
    2. PowerBI recognizes this query as a table. Let's say you name this table BearerTokenTable. This table will only have one cell, let's say the name of the single column is token. This cell will hold the value of the bearer token you obtain from AAD.

 

2. In your regular query where you call your AAD-authenticated API source, pass in the bearer token as the value of the Authorization header as below

  Source = Json.Document(Web.Contents("<URL of your API>",
                 [
                    Headers = [Authorization=BearerTokenTable{0}[token]]
                 ]
    )),

 

Here is an example R script that invokes a Powershell script (in this case named aadClient.ps1).

aadData <- system("powershell C:\\aadClient.ps1", intern=TRUE)
BearerTokenTable<- as.data.frame(matrix(unlist(aadData), ncol=1, byrow=TRUE))
names(BearerTokenTable) <- c('token')

And here's an example PowerShell script for the above (i.e aadClient.ps1)

# Import the required ADAL libraries
[System.Reflection.Assembly]::LoadFrom("${env: ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll") | Out-Null
[System.Reflection.Assembly]::LoadFrom("${env: ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll") | Out-Null

# Please make sure to use the appropriate values for your AAD tenant below $LoginUrl = "https://login.microsoftonline.com/<AAD tenant>" $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $LoginUrl $AppId = "<your application id from AAD>"; $RedirectUrl = "<your AAD redirect url>"; $Resource = "<your API endpoint>"
# Acquire the token from AAD
$authResult = $authContext.AcquireToken($Resource, $AppId, $RedirectUrl, "Auto")
# This is important to output for the R script to capture
Write-Host $authResult.CreateAuthorizationHeader()

 

Make sure to use the right values for the various variables referenced in the PowerShell script above pertaining to your AAD tenant and your API endpoint (the loginURL, AppID and Resource). For more details on how the AAD authentication works see https://docs.microsoft.com/en-us/azure/active-directory/active-directory-authentication-scenarios#ba...

Anonymous
Not applicable

I am new to power BI. Please let me know Is it possible to do it using c# instead of R language and how to do it. Thanks in advance.

nlombardi
New Member

Hi there.  Have you gotten any responses for this question?  We are trying to do the same thing.  We want clients to build reports in Power BI and to retrieve data from our WebAPI REST services via an authorization token.

nlombardi
New Member

Hi there.  Have you gotten any responses for this question?   We are trying to do the same thing.  We want clients to authenticate through Power BI and then send the token to our Web API REST services to retrieve json data.  Thanks.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors