Forum Discussion
irfanharun
7 years agoFrequent Visitor
API Document is very confusing, unable to generate token
Hi all, I'm a PHP developer and trying to get my power BI reports displayed in my webpage for a client. I have the application ID, workspace ID and report ID. I'm trying to get the below boilerp...
TedPattison
7 years agoMicrosoft Employee
Yes, your understanding is correct. Once you obtain an access token, next you call into the Power BI Service API to get embedding data for a report and an embed token. If you use the Power BI API SDK, then you have a librtary to help with this. Here is a simple example of getting the embedding data and an embed token for a report.
public static ReportEmbeddingData GetReportEmbeddingData() {
PowerBIClient pbiClient = GetPowerBiClient();
var report = pbiClient.Reports.GetReportInGroup(workspaceId, reportId);
var embedUrl = report.EmbedUrl;
var reportName = report.Name;
// create token request object
GenerateTokenRequest generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
// call to Power BI Service API and pass GenerateTokenRequest object to generate embed token
string embedToken = pbiClient.Reports.GenerateTokenInGroup(workspaceId,
report.Id,
generateTokenRequestParameters).Token;
return new ReportEmbeddingData {
reportId = reportId,
reportName = reportName,
embedUrl = embedUrl,
accessToken = embedToken
};
}irfanharun
7 years agoFrequent Visitor
Thank you again Ted.
I'm trying to do this only through jquery and my intention was to get the access token through an Ajax call and then use the access token to get the embed token.
However, I'm getting CORS error when trying to get the access token. This works fine for some reason in Postman but not in my html page.
var settings = {
"async": true,
"crossDomain": true,
"url": "https://login.microsoftonline.com/common/oauth2/token",
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded",
"cache-control": "no-cache",
"postman-token": "f89336c3-4da3-37db-7c6e-6ceeec420920",
},
dataType: 'jsonp',
"data": {
"grant_type": "password",
"username": "[email protected]",
"password": "asd!23",
"client_id": "741312d6-f40e-45b4-b491-fff4ea3655c3",
"resource": "https://analysis.windows.net/powerbi/api"
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});Can you please provide an input on this.
Thanks
Irfan