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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
muhammed24
Regular Visitor

Error while creating Access token for Power BI REST API

I was trying to generate an access token for Power BI REST API.

 

I was following the way that is described in this answer.

https://community.powerbi.com/t5/Developer/Power-BI-REST-API-using-postman-generate-embed-token/m-p/...

 

But I am getting the following error - 

    "error""invalid_grant",
    "error_description""AADSTS50126: Error validating credentials due to invalid username or password.\r\nTrace ID: 0237bedd-9747-47d3-bc4d-27bdb1fc5a00\r\nCorrelation ID: e47b45a3-f479-4d09-933d-e6dfeae24ea8\r\nTimestamp: 2023-05-16 06:37:21Z",
    "error_codes": [
        50126
    ]
 
6 REPLIES 6
lmarangio
Helper I
Helper I

@muhammed24 I just found this article that might explain the issue you are facing Authentication issues in Azure HDInsight | Microsoft Learn. In particular 

lmarangio_0-1684222440159.png

Hence either you set up this policy or you use Service Principal as authorization method:)

lmarangio
Helper I
Helper I

Hey @muhammed24 I cannot spot mistakes in you request. My guess would be that you are somehow using an invalid username or password. I therefore suggest either:

 

- Waiting for someone else opinion

- Authenticate with service principal. Generate a secret for the app you alredy have in your AD and then use the following request:

 

POST https://login.microsoftonline.com/{{Tenant ID}}/oauth2/v2.0/token
client_id : your client_id
client_secret: the client secret you just generated
grant_type: client_credentials
scope: https://analysis.windows.net/powerbi/api/.default

 

Be careful, once you generate your app secret, save it, because you will not be able to retreive it again.

 

Let me know if this solution works for you. 

Hi @lmarangio  Thank you. This method is working and I am generating an access token.

 

I still have some issues though, that is, when I use this token as Bearer token for REST API, I am getting a 401 Error.

 

This is the request that I tried with the generated Access Token

 

access_token=****************************
bearertoken=access_token
gatewayId=*********
datasourceId=*************
Headers={
    "Authorization": f"Bearer {bearertoken}"
}

response=requests.request("GET", url=url, headers=Headers)

print(response.status_code, response.content)
 
Result comes as - 401 b''
 
Please let me know if the access token I generated by the method you suggested isn't the correct bearer token. If you know how I can generate a bearer token for this purpose, I appreciate your help.

Hey @muhammed24, I am happy that now is working! You can be sure this is the right way to generate a token to authorize PBI REST API. 

 

The problem here is that you have to setup the right permission in PBI. As you can see from the documentation: Gateways - Get Datasource - REST API (Power BI Power BI REST APIs) | Microsoft Learn there are permission, scope and limitations that you have to take into account.

 

I never worked with datasource and gateway in PBI so I do not in this specific situation how to set up this permission, but that's the problem you have to solve.

 

I suggest you to accept the previous solution and open another post, since this is not anymore about an Access Token. I am sure community will help you in setting up the right permission if you will have problems in doing so.

 

Thanks!

lmarangio
Helper I
Helper I

Hey @muhammed24, from the error descriptions looks like you are using an invalid username or password. Can you please provide the complete description of the request you are using (endpoint, etc)?

 

I recall you that if you are authorizing with username/password (instead of service principal), you have to set "grant_type" to value "password" (instead of "client_credentials).

 

Wait for you reply,

Luigi 

I did put the grant_type as password. My request is as following.

 

POST https://login.windows.net/common/oauth2/token

 

Request body 

grant_type : password

username :  **********

password : ***********

client_id : ********* (client id in the app I created for using REST API)

resource : https://analysis.windows.net/powerbi/api

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

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

July Newsletter

Fabric Community Update - July 2024

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