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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
trt18-sistemas
Advocate IV
Advocate IV

Can't Authenticate against login microsoft online to generate bearer token

Hi there,

 

I'm following these videos tutorials (part 1: https://youtu.be/fjzmz0BNN_g and part 2: https://youtu.be/N8qYRSqRz84) and almost everything is running fine. I can create the app (directly at azure portal as these video seems to be a little bit old), connect to the REST API and list groups/workspaces. But when I try to generate the Bearer token authenticating with my Power BI user, I get the following error message:

"It was not possible to authenticate using the provided credentials. Try again".

 

trt18sistemas_0-1646915855628.png

(

 

I tried to replace the username and password by "App Secret Id" and "App Secret Value", but the error message "Query error. DataSource.Error: Web.Contents fail to get the content of "https://login.microsoftonline.com/commom/oauth2/token/" (400): Bad Request. Detaisl: ..." (image bellow) was launched.

 

trt18sistemas_1-1646915980550.png

 

Could anybody help me understand what is going wrong and how to solve this problem?

 

Below is my code:

 

function GerarToken

 

() =>
let
    username = USER,
    pwd = PASSWORD,
    body = "grant_type=Password&resource=https://analysis.windows.net/powerbi/api&client_id=APP_ID_CLIENT&username="&username&"&password="&pwd,
    Data=Json.Document(Web.Contents("https://login.microsoftonline.com/common/oauth2/token/", [Headers=[#"Content-Type"="application/x-www-form-urlencoded"], Content=Text.ToBinary(body)])),
    access_token = Data[access_token]
in
    access_token

 

 

Query to List Groups

 

let
    token = GerarToken(),
    Source = Json.Document(Web.Contents("https://api.powerbi.com/v1.0/myorg/groups?$top=100", [Headers=[Authorization="Bearer " & token]])),
    value = Source[value],
    #"Converted to Table" = Table.FromList(value, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"id", "isReadOnly", "isOnDedicatedCapacity", "name"}, {"id", "isReadOnly", "isOnDedicatedCapacity", "name"})
in
    #"Expanded Column1"

 

 

Regards

1 ACCEPTED SOLUTION

Replying here the solution found.

 

As posting above, the @v-henryk-mstf suggestion works to generate the bearer token. After using its example code, another error occurred. It was happening because, for some reason, the permission to use API, accessible at "Administration Portal >> Tenant Configuration >> Allow principal services to use Power BI  API", must be set as "All organization" (yellow highlighted), instead of "Specific groups" (red underlined).

 

trt18sistemas_0-1649378099071.png

 

View solution in original post

4 REPLIES 4
v-henryk-mstf
Community Support
Community Support

Hi @trt18-sistemas ,

 

You can try to use following M query code if they help with you scenario:

let

url = "https://login.microsoftonline.com/xxxxxxx/oauth2/token",

GetJson =

Web.Contents(

url,

[

Headers = [

Accept = "application/json",

ContentType = "application/x-www-form-urlencoded"

],

Content =

Text.ToBinary(

"grant_type=client_credentials&

client_id=xxxxxxx&

client_secret=xxxxxxx&

scope=xxxxxx"

)

]

),

token = Json.Document(GetJson)[access_token],

Result =

Web.Contents(

"https://xxxxx.xxx.com",

[

Headers = [

#"Content-Type" = "application/json",

Authorization = "Bearer " + token,

RelativePath = "/xxxxx/xxxxx"

]

]

)

in

Result

 

Regards,

Xiaoxin Sheng

Hi @v-henryk-mstf ,

 

Thanks for your time.

 

I've used your code as example and it generated a token sucessfully. Although, When I try to use the token to list my workspaces (groups), the API reply with a "Forbiden" error message.

 

Below is my code following your example (some variables were omitted for securety behalf):

 

trt18sistemas_0-1647304626114.png

 

Here is the output:

 

trt18sistemas_1-1647304738439.png

 

 

To make sure that it's not some issue on Power BI, I've tried the same on Postman and, as in Powe BI, I could generate a token, but when I try to query the list of workspaces  a "403 Forbidden" message is replied.

 

trt18sistemas_2-1647304896414.png

 

 

For some reason that I don't know why, the tokens generated by Azure AD doesn't seems to work with Power BI.

trt18-sistemas
Advocate IV
Advocate IV

One more info. I tried the same thing by using Postman online and it worked, but using Powe BI the autentication error message continues.

 

trt18sistemas_0-1647203438773.png

 

I would be greatefull for any help.

Replying here the solution found.

 

As posting above, the @v-henryk-mstf suggestion works to generate the bearer token. After using its example code, another error occurred. It was happening because, for some reason, the permission to use API, accessible at "Administration Portal >> Tenant Configuration >> Allow principal services to use Power BI  API", must be set as "All organization" (yellow highlighted), instead of "Specific groups" (red underlined).

 

trt18sistemas_0-1649378099071.png

 

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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