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

Get Bearer token in Power Query is working in Power BI Desktop but not in Power BI Service

Hi,

I have a requirement to get the list of Power BI workspaces by calling a Power BI Rest API through bearer token in a Web connector. But, I am using the following query to get the bearer token dynamically and passing that token to an API call in its headers. It is working fine in Power BI Desktop but when the same report is published into Power BI Service, there the data source configuration is giving me a 400 error saying "Failed to update data source credentials. The credentials provided for the web source are invalid".

1.  So to implement dynamic bearer token creation, user has to register a 3rd party application in their AAD and get the App Id which is the client id.

2. Then enable Power BI API read all permissions in the registered app. This is because I am trying to get the list of workspaces in Power BI through Power BI Rest API.

3. Now the power query goes like this:

 

let
// 1. Authentication set up

// a. Get the bearer token with a Http POST request
auth_key = "grant_type=Password&resource=https://analysis.windows.net/powerbi/api&client_id=blablablac&username=xyz@***.com&password=blablabl...",

Data=Web.Contents("https://login.microsoftonline.com/common/oauth2/token/", [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(auth_key)]),

Jsonfile= Json.Document(Data),
access_token= Jsonfile[access_token],


// b. Get the token from 1 and pass it in Headers of Http GET request
Source = Json.Document(Web.Contents("https://api.powerbi.com", [RelativePath = "/v1.0/myorg/groups", Headers=[Authorization="Bearer "&access_token]])),
value = Source[value],
#"Converted to Table" = Table.FromList(value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column2" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "name"}, {"id", "name"})
in
#"Expanded Column2"

 

This query is working fine in Power BI Desktop, but not in Power BI Service. Don't know the reason for this error. Could someone help me in this.

 

Thanks

Lakshmi Koduri

1 REPLY 1
v-deddai1-msft
Community Support
Community Support

Hi  @Lakshmi_Koduri ,

 

You can follow the similar blog providing details about how to connect to REST API via OAuth2 in Power BI. You can following the instructions in https://chris.koester.io/index.php/2015/07/16/get-data-from-twitter-api-with-power-query/ to connect to your data source .

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,

Dedmon Dai

 

 

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