Reply
Highlighted
Frequent Visitor
Posts: 11
Registered: ‎06-15-2017

How To: Get embed token using Get/Post only

[ Edited ]

 

Step 1: Authorize App

Register App

I used a native app, it was the simplest solution. You can use a server-web app, but you will need to add client_secret anywhere you see client_id in this walkthrough. You will need to at least have read permissions enabled.

 

Step 2: Get Access Token (post)

 

POST: https://login.microsoftonline.com/common/oauth2/token
data: {
    grant_type: password
    scope: openid
    resource: https://analysis.windows.net/powerbi/api
    client_id: {Client ID}
    username: {PBI Account Username}
    password: {PBI Account Username}
}

--Returns Json:
 
{
    "token_type": "Bearer",
    "scope": "Report.Read.All ...",
    "expires_in": "xxxx",
    "ext_expires_in": "0",
    "expires_on": "xxxxxxxxxx",
    "not_before": "xxxxxxxxxxx",
    "resource": "https://analysis.windows.net/powerbi/api",
    "access_token": "eyJ0eXAi...",
    "refresh_token": "AQABA...",
    "id_token": "eyJ...."
}

 

 

Step 3: Get Report details:

This can be done two different ways. The simplest way is to navigate to your report on the Power Bi website and pull the report id and group id from the url.

https://app.powerbi.com/groups/{GROUP ID}/reports/{REPORT ID}/ReportSection

I needed to pull different reports from a given group, so here is the get request to do that.

 

GET https://api.powerbi.com/v1.0/myorg/groups/{GROUP ID}/reports
headers = {
Authorization: Bearer + {access_token from first post}
}

Returns Json:
{
    "@odata.context": "http://wabi-west-us-redirect.analysis.windows.net/v1.0/myorg/groups/.../$metadata#reports",
    "value": [
        {
            "id": "...",
            "modelId": 0,
            "name": "...",
            "webUrl": "...",
            "embedUrl": "https://app.powerbi.com/reportEmbed?reportId={REPORT ID}&groupId={GROUP ID}",
            "isOwnedByMe": true,
            "isOriginalPbixReport": false,
            "datasetId": "..."
        },
... Repeated for other Reports in Group
}

 

Step 4: Get Embed token

In the old Power Bi server (through Azure) you could encode your own embed token, in the September 2017 update Microsoft started to require that you use the rest api or one of the available SDKs.  In the "data" section you can pass arguments to implement row level security (link).

 

 

 

POST https://api.powerbi.com/v1.0/myorg/groups/{GROUP ID}/reports/{REPORT ID}/GenerateToken
headers = {
Authorization: Bearer + {access_token from first post}
Content-Type:application/json; charset=utf-8
Accept:application/json
}
data= {
  "accessLevel": "View",
  "allowSaveAs": "false"
}

Returns Json:
{
    "@odata.context": "http://wabi-west-us-redirect.analysis.windows.net/v1.0/myorg/groups/{GROUP_ID}/$metadata#Microsoft.PowerBI.ServiceContracts.Api.V1.GenerateTokenResponse",
    "token": "H4sIAAAAAAA...",
    "tokenId": "...",
    "expiration": "yyyy-mm-ddTxx:xxxxx"
}

 

Step 5: Test

  Go to the Microsoft Power BI Embedded Sample site (link) and test your report. 

The input fields are as follows:

You can also test the embedded report using the following :

<html>
<script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/jquery/dist/jquery.js"></script>
<script src="https://microsoft.github.io/PowerBI-JavaScript/demo/node_modules/powerbi-client/dist/powerbi.js"></script>
<script type="text/javascript">

window.onload = function () {
var embedConfiguration = {
    type: 'report',
	accessToken: '{access_token}',
	embedUrl: 'https://app.powerbi.com/reportEmbed?reportId={REPORT ID}&groupId={GROUP ID}',
	id:'{REPORT ID}',
settings: {
              {settings_ from link}
            }
	}; 
var $reportContainer = $('#dashboardContainer');
var report = powerbi.embed($reportContainer.get(0), embedConfiguration);
}

function reloadreport(){
	var element = $('#dashboardContainer');
	alert(element);
	var report = powerbi.get(element);
	report.reload().catch(error => {console.log(error)  });
};
</script> 
<div id="dashboardContainer"></div>
</html>  

Notes:

Available settings can be found here.

 

Disclaimer:

This code comes with no warranty and no guarantees.  It is HIGHLY likely that Microsoft will change something and this walkthrough will become depreciated. Please check your PowerBi agreement to make sure that the steps listed in this walkthrough adhere to the agreed upon terms.

 

Tags:

Python, embedded, token, access token, get, post, REST API, ruby, PHP

 

 EDIT 1:

Added space to prevent Smiley Surprised and Smiley Tongue in code.

 

Edit 2:

Turned down sarcasm and added links.

Moderator
Posts: 3,051
Registered: ‎03-06-2016

Re: How To: Get embed token using Get/Post only

@adamwallace3

Thanks for your sharing. It really helps.

Frequent Visitor
Posts: 3
Registered: ‎03-27-2018

Re: How To: Get embed token using Get/Post only

I tried with native app approach and it works when I use UserCredentials (username & password). But if I use a web app and use client id , client secret it always says unauthorised. Is there a limitation that PowerBI APIs only works with user credentials?

Frequent Visitor
Posts: 5
Registered: ‎03-28-2018

Re: How To: Get embed token using Get/Post only

I am able to follow thorugh until the last step. When  I use the HTML code with the Javascript nothing happens. I am able to perform all the following steps and obtain all the needed information. I am also able to input my token information into the sample website linked in the post and my report loads.  

This is in a simple angular app. What do I need to do after I have my embeded token in order to display the ebmedded report?

 

Frequent Visitor
Posts: 4
Registered: ‎05-18-2016

Re: How To: Get embed token using Get/Post only

This has been a great help, as I am in a PHP shop, and all of the examples on the Power BI Developer site are in C#. 

 

Thanks so much.

Frequent Visitor
Posts: 5
Registered: ‎04-26-2018

Re: How To: Get embed token using Get/Post only

Hi there,

 

You need to pass the PowerBI credential because that is how the licensing model of Power BI works. That was MS can satisfy that the user who is trying to embed report is Power BI Pro user and that has the Power Bi Embedded capaciy.

Frequent Visitor
Posts: 13
Registered: ‎06-13-2018

Re: How To: Get embed token using Get/Post only

Thanks for sharing and sorry for the novice question but how and where can I run this code?

Frequent Visitor
Posts: 4
Registered: ‎05-18-2016

Re: How To: Get embed token using Get/Post only

This isn't code, but rather pseudocode. You would need to implement it in whatever language you are writing in.

 

The best thing to do is probably try Postman, at https://www.getpostman.com/. You can use that to send GET/POST commands to the API.

Frequent Visitor
Posts: 13
Registered: ‎06-13-2018

Re: How To: Get embed token using Get/Post only

Thank you! I was able to run it!

Frequent Visitor
Posts: 13
Registered: ‎06-13-2018

Re: How To: Get embed token using Get/Post only

[ Edited ]

Steps 1 and 2 run as expected, when I run step 3 & 4 it just returns blank, any ideas?

Sorry, I am using Postman and I added the quotes around the token, once I removed the quotes it worked fine.