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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
insoldev
Helper I
Helper I

Power BI REST API using postman - generate embed token.

Hello - currently working through trying to get an embed token using the Power BI rest api. I am following the guidance as on this page. https://msdn.microsoft.com/en-us/library/mt784614.aspx.

 

ultimately, I am trying to achieve the outcome of using javascript to host a Power BI report in the new model as per https://community.powerbi.com/t5/Developer/Embedding-without-hard-coding-master-credentials-app-owns... as per @Eric_Zhang suggestion.

 

I am not getting an embed token - I am simply getting this response. Clearly I've missed something - I am using my client secret as the bearer token.

 

postman body.PNG

1 ACCEPTED SOLUTION
Eric_Zhang
Employee
Employee


@insoldev wrote:

Hello - currently working through trying to get an embed token using the Power BI rest api. I am following the guidance as on this page. https://msdn.microsoft.com/en-us/library/mt784614.aspx.

 

ultimately, I am trying to achieve the outcome of using javascript to host a Power BI report in the new model as per https://community.powerbi.com/t5/Developer/Embedding-without-hard-coding-master-credentials-app-owns... as per @Eric_Zhang suggestion.

 

I am not getting an embed token - I am simply getting this response. Clearly I've missed something - I am using my client secret as the bearer token.

 

 


@insoldev

The bearer token requires an access token, not the client secret. You need to follow this link to register a Native app rather than a server-side web app for Power BI Embedded cases and grant sufficient permissions.

 

Then to get the access token, you can call the POST API in POSTMAN as

POST /common/oauth2/token HTTP/1.1
Host: login.windows.net
Cache-Control: no-cache 
Content-Type: application/x-www-form-urlencoded

client_id={client id}&grant_type=password&resource=https%3A%2F%2Fanalysis.windows.net%2Fpowerbi%2Fapi&username={your power bi account}&password={your Power BI password}

Capture.PNG

 

With the above access_token, you can call the GenerateToken API.

 

By the way, using POSTMAN is recommended for testing/debug purpose, what would you like in your case?

View solution in original post

15 REPLIES 15
Eric_Zhang
Employee
Employee


@insoldev wrote:

Hello - currently working through trying to get an embed token using the Power BI rest api. I am following the guidance as on this page. https://msdn.microsoft.com/en-us/library/mt784614.aspx.

 

ultimately, I am trying to achieve the outcome of using javascript to host a Power BI report in the new model as per https://community.powerbi.com/t5/Developer/Embedding-without-hard-coding-master-credentials-app-owns... as per @Eric_Zhang suggestion.

 

I am not getting an embed token - I am simply getting this response. Clearly I've missed something - I am using my client secret as the bearer token.

 

 


@insoldev

The bearer token requires an access token, not the client secret. You need to follow this link to register a Native app rather than a server-side web app for Power BI Embedded cases and grant sufficient permissions.

 

Then to get the access token, you can call the POST API in POSTMAN as

POST /common/oauth2/token HTTP/1.1
Host: login.windows.net
Cache-Control: no-cache 
Content-Type: application/x-www-form-urlencoded

client_id={client id}&grant_type=password&resource=https%3A%2F%2Fanalysis.windows.net%2Fpowerbi%2Fapi&username={your power bi account}&password={your Power BI password}

Capture.PNG

 

With the above access_token, you can call the GenerateToken API.

 

By the way, using POSTMAN is recommended for testing/debug purpose, what would you like in your case?

Above solution only mentioned using Master User account to authenticate and get access token, in the request body we need provide username and password, Request URL is https://login.microsoftonline.com/common/oauth2/token. Actually we can use either https://login.microsoftonline.com/common/oauth2/token or https://login.microsoftonline.com/{tenantID}/oauth2/v2.0/token for master user. 

Note: we need replace "{tenantID}" with cx tenant ID. 

 

Furthermore, we can also use Service Principal to authenticate and get access token: the request URL can only be https://login.microsoftonline.com/{tenantID}/oauth2/v2.0/token

In the request body, we need set grant_type as client_credentials, and provide client_id and client_secret. After Create an Azure AD app, we will have Application ID(client_id) and secret. 

JerryTang_0-1674224888531.png

 

 

Hi jerry,

I am trying the same in postman. but with this token I am not able to generate embed token in postman.

sharing response which I am getting when generating access token. can please confirm this is the correct response and you are getting the same.

Burhan_Jiru_0-1692197874697.png

If this is the correct response can you please guide me on how to generate embed token in Postman?

 

thank you 

Hello, I am getting the same error. Did you manage to make this work yet with client credentials?

I'm facing some troubles in to replicate what has been done here.

I had already register the app on Azure AD and create the workspace in Power BI as mention here: Get started with Power BI Embedded - Power BI | Microsoft Learn

 

But when I use your steps to get access token by Postman, I receive that the hostname is not correct: 

ivanSantos16_0-1695380320587.png

 

Can somebody help me please?

what is cx tenant ID

Anonymous
Not applicable

Thank you so much, was able to get the access_token using this method in postman.

 

When you say "With the above access_token, you can call the GenerateToken API.", can you give us an example of how we would use the access_token from the call to https://login.windows.net/common/oauth2/token to one of the post methods?

 

For example, I am trying to embed a report in .net core.  I want to use this rest api method: Reports GenerateTokenInGroup

 

POST https://api.powerbi.com/v1.0/myorg/groups/{groupId}/reports/{reportId}/GenerateToken

 

with some parameters (accessLevel, allowSaveAs, datasetId, identities) in the request body.  How would I use the access_token from the first API call with the second one (POST https://api.powerbi.com/v1.0/myorg/groups/{groupId}/reports/{reportId}/GenerateToken)?

 

Thank you!

@Anonymous did you find a solution to this, are you able to share a common approach for postman?

Anonymous
Not applicable

I am getting this response from Postman:

"error": "invalid_grant", "error_description": "AADSTS70002: Error validating credentials. AADSTS50126: Invalid username or password\r\nTrace ID: c3c862a5-440b-43ba-a105-7efea28b2800\r\nCorrelation ID: 7df5b009-805a-4738-8c81-b9eca778054a\r\nTimestamp: 2018-10-18 20:14:11Z", "error_codes": [ 70002, 50126 ],

 

but I know my login/pw are good because if I use the same credentials/client_id with the Python library PyPowerBi (https://github.com/cmberryau/pypowerbi)

 

I can connect and make API calls, so the problem has to be with the Postman setup:

 

POST: https://login.microsoftonline.com/common/oauth2/token

 

BODY:

grant_type:password

client_id:{{client_id}}

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

scope:openid

username:{{username}}

password:{{password}}

Hi @Eric_Zhang - I wanted to thank you for your contribution. This worked for me and gave me a really good steer. I have used Azure Functions apps to provide the server side functionality.

Thanks for your response - I'm just testing to make sure I have the components working correctly.

First I would like to say sorry that I don't have an exact solution about this but I feel like that Whatever I will suggest here it would be helpful or it will work.

 

Just go through the article REST API in Power BI where all things are described in the wonderful and easy way. Diagrams and video are also available in this article which will help to learn in a more easier way.

 

Hope it Helps 🙂

@Yikun, that isn't even close to relevant - I'm talking about using the Power BI API in an embedded application, not using an API as a data source in Power BI.

First I would like to say sorry that I don't have an exact solution about this but I feel like that Whatever I will suggest here it would be helpful or it will work.

 

Just go through the article REST API in Power BI where all things are described in the wonderful and easy way. Diagrams and video are also available in this article which will help to learn in a more easier way.

 

 

Helpful resources

Announcements
March Fabric Community Update

Fabric Community Update - March 2024

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

Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.