Forum Discussion
How To: Get embed token using Get/Post only
Step 1: Authorize 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:
- Embed Token: {token from final post}
- Embed URL: https://app.powerbi.com/reportEmbed?reportId={REPORT ID}&groupId={GROUP ID}
- Report Id: {REPORT ID}
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 :o and :p in code.
Edit 2:
Turned down sarcasm and added links.
35 Replies
- HomeslyceRegular Visitor
After HOURS of research and tests and everything nothing worked, I was goign to flip that table. Your post saved my week, so far the best how to I've seen on the subject.
Microsoft should hire you!
For those wondering, you better start with Postman or any other API test application to test the 1st and 3rd section.
- Eric_ZhangMicrosoft Employee
Thanks for your sharing. It really helps.
- nitin_spRegular Visitor
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.
- unnieNew Member
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?
- johnwhelNew Member
I am also getting a 403 Unauthorized error when I use client id and secret to access the report from Power Bi (last step).
I am able to obtain the embedded token, but not the report.
any ideas?
- AnonymousNot applicable
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? - andyparkersonAdvocate III
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.
- azulcoreFrequent Visitor
Thanks for sharing and sorry for the novice question but how and where can I run this code?
- andyparkersonAdvocate III
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.
- azulcoreFrequent Visitor
Thank you! I was able to run it!
- azulcoreFrequent Visitor
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.
- riyankaRegular Visitor
could you please send screenshot of step no 2 in postman
- zachmillerFrequent Visitor
Here's how Step 2 should be set up in Postman, keeping in mind that I obviously censored the 4 empty values.
- hsheikhaliFrequent Visitor
I get a nasty error saying:
400 Client Error: Bad Request for url: https://login.windows.net/common/oauth2/token
{
"timestamp": "2018-09-14 01:18:04Z",
"trace_id": "a6c902bb-2263-44ce-8675-ce1fa7196c00",
"correlation_id": "94d0a3bb-0b79-4cc9-9d52-bce7973c438e",
"error_description": "AADSTS70002: Error validating credentials. AADSTS50126: Invalid username or password\r\nTrace ID: a6c902bb-2263-44ce-8675-ce1fa7196c00\r\nCorrelation ID: 94d0a3bb-0b79-4cc9-9d52-bce7973c438e\r\nTimestamp: 2018-09-14 01:18:04Z", 01:30 Dur 21:16:15
"error": "invalid_grant",
"error_codes": [
70002,
50126
]
}everytime. I registered a native app however, everytime I try to use my UserCredentials.. I get the above error... Any guidance would be great.
- mohsin321Frequent Visitor
i have successfully completed all the three steps as described in the tutorial bu at step 4 when i used my access token to generate the embed for me, HTTP gives the blank reponse there is no json array in the response as described in the tutorial and also it does not give any error .Any help regarding this issue would be appreciated thankx
- azulcoreFrequent Visitor
All of the steps work except for the last step. I entered all the values in the https://microsoft.github.io/PowerBI-JavaScript/demo and it works well. But using your html code at the end just gives a blank page, any ideas? I use clientid and clientsecret if that makes a difference
- ashleymartinFrequent Visitor
Step 4 is not working for me. I was able to get the access token in step 2 and then able to run step 3. For step 4 I entered the header information into postman under header and the data information under the body but I am getting this error:
{
"error": {
"code": "BadRequest",
"message": "Bad Request",
"details": [
{
"message": "Unexpected character encountered while parsing number: W. Path '', line 1, position 6.",
"target": "request"
}
]
}
}
]
}
}Is anyone else experiencing this or know of a way I can fix this? Also for reference I am doing a server-web app and have added the client secret into the first step, not sure if I would need to add anything in this 4th step too or if there is another reason it is showing this error.
- matthewgouwsNew Member
Hi,
Did you find the solution to the error you were getting in Step 4 ?
Thanks in advance! - matthewgouwsNew Member
Hi,
Did you manage to find a solution to the error you were getting in step 4 of this guide?
Regards,Matt
- zachmillerFrequent Visitor
ashleymartin The "data= {}" section of step 4 needs to be put into the "Body" tab on Postman. You can click the "raw" radio button to be able to paste it in directly. The headers go in the headers tab as well. Other than that, make sure that the access token you have is not expired. From there, you can use the embed token where ever you deem appropriate.
- aacoRegular Visitor
Hi,
When executing the step 4 in Postman (adding the Authorization token in the headers and the Data in the body) I'm getting the following error:
{
"error": {
"code": "",
"message": "The request entity's media type 'multipart/form-data' is not supported for this resource."
}
}Also, when running step 5 in Chrome, I get this error message:
GET https://wabi-north-europe-redirect.analysis.windows.net/metadata/cluster 403 (Forbidden)
From step 1 to step 3 all works as described. Is someone else getting the same error message? Any ideas of how to fix it or where it should be coming from?
Many thanks
- aacoRegular Visitor
So, I figured out that I wasn't using the Body > raw > JSON (application/json) option in Postman. Now I do it but am getting a different error:
{
"error": {
"code": "Unauthorized",
"message": "Report.Read.All Dataset.Read.All"
}
}For info, steps 1 to 3 are still OK and permissions on the Azure App Registration side for 'Power BI Service' are all set to 'DELEGATED' (19 in total).
Thanks again.
- AnonymousNot applicable
aaco Did you find the source of this issue please ?