So, I've created a report on Power BI. I want to embed this report to my MVC site. Here is the code:-
private static readonly string ClientID = ConfigurationManager.AppSettings["ClientID"];
private static readonly string ClientSecret = ConfigurationManager.AppSettings["ClientSecret"];
private static readonly string RedirectUrl = ConfigurationManager.AppSettings["RedirectUrl"];
private static readonly string AADAuthorityUri = ConfigurationManager.AppSettings["AADAuthorityUri"];
private static readonly string PowerBiAPI = ConfigurationManager.AppSettings["PowerBiAPI"];
private static readonly string PowerBiDataset = ConfigurationManager.AppSettings["PowerBiDataset"];
private static readonly string baseUri = PowerBiDataset;
private static string accessToken = string.Empty;
public string GetAccessToken(string authorizationCode, string clientID, string clientSecret, string redirectUri)
{
TokenCache TC = new TokenCache();
string authority = AADAuthorityUri;
AuthenticationContext AC = new AuthenticationContext(authority, TC);
ClientCredential cc = new ClientCredential(clientID, clientSecret);
return AC.AcquireTokenByAuthorizationCodeAsync(
authorizationCode,
new Uri(redirectUri), cc).Result.AccessToken;
}
public void GetAuthorizationCode()
{
var @params = new NameValueCollection
{
{"response_type", "code"},
{"client_id", ClientID},
{"resource", PowerBiAPI},
{ "redirect_uri", RedirectUrl}
};
var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString.Add(@params);
Response.Redirect(String.Format(AADAuthorityUri + "?{0}", queryString));
}
public ActionResult Index()
{
if (Request.QueryString["code"] != null)
{
Session["AccessToken"] = GetAccessToken(
HttpContext.Request["code"],
ClientID,
ClientSecret,
RedirectUrl);
}
if (Session["AccessToken"] != null)
{
accessToken = Session["AccessToken"].ToString();
System.Net.WebRequest request = System.Net.WebRequest.Create(
String.Format("{0}/Reports",
baseUri)) as System.Net.HttpWebRequest;
request.Method = "GET";
request.ContentLength = 0;
request.Headers.Add("Authorization", String.Format("Bearer {0}", accessToken));
using (var response = request.GetResponse() as System.Net.HttpWebResponse)
{
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
{
PBIReports Reports = JsonConvert.DeserializeObject<PBIReports>(reader.ReadToEnd());
if (Reports.value.Length > 0)
{
PBIReport report = Reports.value[13];
return View(report);
}
}
}
}
GetAuthorizationCode();
return View();
}
On Redirecting to "this", it goes for power bi login page and after I sign in it redirects back to this page (as homepage and redirect URL are same). After getting all the report data, after some time an error message comes up saying Unhandled exception at line 5153, column 11 in http://localhost:34244/Scripts/powerbi.js 0x800a1391 - JavaScript runtime error: 'Promise' is undefined